Calculator Help


#1


I am trying to make a calculator for fun but when I prompt for numbers on both add and subtract. The data is held like a string rather than a number. There are no syntax errors.


var answer1 = prompt('What is the first number?');
var answer2 = prompt('What is the second number?');
var addition = function(number1, number2) {
   console.log('Your answer is:')
   console.log(number1 + number2);
};
addition(answer1,answer2);


#2

And what is the problem?


#3

When i try to add the two prompted numbers they just are put together like strings instead of numbers. I want 4 + 6 to = 10 not 46


#4

The number data type has that property. Can you turn a string into a number?


#5

You can use the
"Number()" method to convert the output to "numbers" at call
e.g addition(Number(answer1), Number(answer2));


#6

Thanks a lot, but where in the code would that go?


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.