Chris
Chris

Reputation: 944

JavaScript operator + doesn't work normally

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>

Upvotes: 2

Views: 126

Answers (3)

Mark Giblin
Mark Giblin

Reputation: 1106

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));

Upvotes: 0

Chris Wheeler
Chris Wheeler

Reputation: 1716

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.

Upvotes: 2

Sterling Archer
Sterling Archer

Reputation: 22395

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);

Upvotes: 1

Related Questions