3

i'm having some trouble with the parseInt function or + operand. I want to take in two numbers, multiply one by a third number and add them together. Instead of adding the numbers it appends one number to another.

<script language = 'JavaScript'>
function calculate_total()
{
var a = 0;
var b = 0;
var t = 0;

    parseInt(a = prompt('Pay 1'), 10);
//Input of 10
        if(isNaN(a))
        {
            alert('Error A');
        }
//No Error

    parseInt(b = prompt('Pay 2'), 10);
//input of 12
        if(isNaN(b))
        {
            alert('Error B');
        }
//No Error

    parseInt(t = (a * 20 + b), 10);
        if(isNaN(t))
        {
            alert('Error T');
        }
        else
        {
            alert('Total Pay: ' + t);
//expected answer 212
//Actual Answer 20012
        }
//No Error
}
calculate_total();
</script>
thefourtheye
  • 233,700
  • 52
  • 457
  • 497

6 Answers6

8
parseInt(a = prompt('Pay 1'), 10);
...
parseInt(b = prompt('Pay 2'), 10);
...
parseInt(t = (a * 20 + b), 10);

Here a, b and t, all have got string data only and when it is converted to an int, its discarded immediately. So, fix them like this

a = parseInt(prompt('Pay 1'), 10);
...
b = parseInt(prompt('Pay 2'), 10);
...
t = a * 20 + b;

According to ECMA Script's specifications for Additive operation,

7 If Type(lprim) is String or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)

So when you use a string and number with + operator, result will be concatenation of both the operands.

According to ECMA Script's specifications for Multiplicative operation,

  1. Let left be the result of evaluating MultiplicativeExpression.
  2. Let leftValue be GetValue(left).
  3. Let right be the result of evaluating UnaryExpression.
  4. Let rightValue be GetValue(right).
  5. Let leftNum be ToNumber(leftValue).
  6. Let rightNum be ToNumber(rightValue).

* operator basically converts both the operands to numbers. So, the result will be proper even if both the operands are numbers in strings.

You can confirm the above mentioned things with this

var a = "100", b = "12";
console.log(a * 20);      // Prints 2000, both operands are converted to numbers
console.log(a * 20 + b);  // Prints 200012, as b is string, both are concatenated
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
4

I'm not a Javascript expert by any means, but you seem to be ignoring the result of parseInt, instead storing just the result of prompt() in your a, b and t variables. I'd expect this:

parseInt(a = prompt('Pay 1'), 10);

to be:

a = parseInt(prompt('Pay 1'), 10);

(And the same for the other prompts.)

At that point, the variables' values will be the numbers rather than the strings, so + should add them appropriately.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

You parsing result which is already wrong due to appending as string:

try updating following statements:

a = parseInt(prompt('Pay 1'), 10);
b = parseInt(prompt('Pay 2'), 10);
t = a * 20 + b;  // no need to parse as a and b already integers
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
1

try

a = parseInt(prompt('pay 1'),10);

etc. for b and t. Now you declare a as prompt('pay 1') and not as the int value returned by parseInt.

user1176420
  • 113
  • 2
  • 9
1

parseInt() returns an integer value. And you dony need parseInt(string, 10), for parseInt decimal system used by defaults.

a = parseInt(prompt('Pay 1'));
b = parseInt(prompt('Pay 2'));
t = a * 20 + b;
Roman Izosimov
  • 210
  • 1
  • 9
-1

I've fixed the code for you:

<script language = 'JavaScript'>
function calculate_total()
{
var a = 0;
var b = 0;
var t = 0;

    a = parseInt(prompt('Pay 1'), 10);
//Input of 10
        if(isNaN(a))
        {
            alert('Error A');
        }
//No Error

    b = parseInt(prompt('Pay 2'), 10);
//input of 12
        if(isNaN(b))
        {
            alert('Error B');
        }
//No Error

    t = parseInt(a * 20 + b, 10);
        if(isNaN(t))
        {
            alert('Error T');
        }
        else
        {
            alert('Total Pay: ' + t);
//expected answer 212
//Actual Answer 20012
        }
//No Error
}
calculate_total();
</script>
vooD
  • 2,881
  • 2
  • 25
  • 34