2

Need some help with this simple code, if we will input the value for "work experience" for example 3 and the salary "1000" by the condition the salary should add 10% to the initial salary so the result should be "1100" but in my formula it shows the result like 1000250, i observed if i change the symbol "+" into "-" it shows correctly "900", what i am doing wrong?

if (age>=3 && age<10) {
    var increase_1;
    var salary_2;
    increase_1=(salary*10)/100;
    salary_2=salary+increase_1;

    document.write('<h4>'  +'Proceeding from work experience the new salary was increase by 10%:\t'+ +salary_2+  '<\h4>');

Also if i am using the salary a defended value like "salary=1000;" the program works with no problem.. waiting for some answers, thx

<html>
    <head>
    </head>    
    <script>    
        function doStuff()
        {
            var nameAge = document.getElementById("ageInput");
            var age = nameAge.value;
            var nameSalary = document.getElementById("salaryInput");
            var salary = nameSalary.value;                    
            document.write('<h2>' +'Age experience:\t'+ +age+ '<\h2>');

            document.write('<h2>' +'Starting salary ($):\t'+ +salary+ '<\h2>');

            if (age>=3 && age<10) {
                var increase_1;
                var salary_2;
                increase_1=(salary*10)/100;
                salary_2=salary+increase_1;                               
                document.write('<h4>'  +'Proceeding from work experience the new salary was increase by 10%:\t'+ +salary_2+  '<\h4>');
                        }      
            else if (age>=10 && age<20){
                var increase_2;
                var salary_3;
                increase_2=(salary*25)/100;
                salary_3=salary+increase_2;
                document.write('<h4>' +'Proceeding from work experience the new salary was increase by 25%:\t'+ +salary_3+  '<\h4>');
            }
            else if (age>=20){                                
                document.write('<h4>' +'Proceeding from work experience you get a prize:'+  '<\h4>');
                document.write('<img src="http://3.bp.blogspot.com/-2T_AGEs19_4/T_c2ERHsJeI/AAAAAAAIK9E/MGAQAa9ppDE/s800/2013-Mercedes-G-Class-AMG-011.jpg">');
            }
            else  {
                document.write('<h4>' +'Proceeding from work experience the  salary is:\t' +salary+'<\h4>');    
            }
        }
    </script>
    <h1>Please enter your work experience(years)</h1>
    <input id="ageInput" type="text">
    <h1>Please enter your salary($)</h1>
    <input id="salaryInput" type="text">      
    <input type="button" value="Submit" onClick="doStuff()">      
</html>
5
  • 2
    Welcome to JavaScript. Make sure you're operating on a number first (e.g., not a string). Commented Nov 25, 2013 at 17:16
  • Cast salary to an int, it looks like it's reading it as a string Commented Nov 25, 2013 at 17:16
  • try doing 100.0 instead of 100 ? Commented Nov 25, 2013 at 17:17
  • 3
    I don't know why this question is voted down, probably it seems like this guy is new to JavaScript, everyone started at some or the other point and got stuck at some foolish thing, doesn't mean we downvote and demotivate, he tried, and than he asked for help, if you cannot help, than don't downvote either.. Commented Nov 25, 2013 at 17:19
  • possible duplicate of How to add two values in javascript Commented Nov 25, 2013 at 17:30

3 Answers 3

2

JavaScript is seeing some values which are taken from input boxes as strings of text rather than numeric values, and simple concatenating them.

If you are reading a value from a input box and want to use it in an equation, you need to run it though parseInt() first. e.g.

var age = parseInt(nameAge.value, 10);

Or if you want to use decimal values (floats) you need to run it through parseFloat()

var salary = parseFloat(nameSalary.value);

Passing the radix (10) as the second parameter to parseInt() will prevent older browsers which use ECMAScript less than version 5 from interpreting numbers starting with a 0 as octal values.

Sign up to request clarification or add additional context in comments.

1 Comment

Always pass the radix to parseInt.
1

Parse your input to an int (or a float if you want decimals!), it's reading as a string, thus concatenating.

var salary = parseInt(nameSalary.value);
var age = parseInt(nameAge.value);

Comments

0

To get a numeric from a form field, you have to get it converted to a number, the quickest way to do that is to subtract zero from that variable.

salary-=0;
increase_1=salary+((salary*10)/100);
salary_2=salary+increase_1;

Your salary of "1000" will result in an (int)1000 that is then *10 = 10000 then /100 = 100 and added to the original sum of 1000 to make 1100.

You could try using a function or a prototype to do the leg work for you,

Number.prototype.percent = function(p){
    return ((this-0)*p)/100;
} 

then you can simply... increase_1=salary+(salary.percent(10));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.