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>
1

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this works and the explanation is easy to understand
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.

1 Comment

Thank you this works and the explanation is easy to understand
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

Comments

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;

Comments

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.

Comments

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

1 Comment

I think the OP would learn more if you also explained what you changed and why.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.