Turning strings into numbers?


#1


Not really anything to do with the exercises, I am just trying to make a calculator for fun. My main problem is that when I prompt the user for two numbers, they are stored as strings. FWI i am only on the fourth section and am not that advanced. No syntax errors.


var userAnswer1 = prompt('What is your first number?');
var userAnswer2 = prompt('What is your second number?');
var addition = function(x, y) {
    console.log('Your Number is: ');
    console.log(x + y);
}
addition(userAnswer1,userAnswer2);


#2

i think this is what you are looking for:

Good luck! Such a project will improve your coding skill :slight_smile:

Or do you also want to allow floats? (decimal numbers)

just like there is parseInt() which parses the string to integer, there is also parseFloat():

One problem with doing this, what if the user doesn't enter a number? You might have to validate this. Some mdn documentation to help you forward:

MDN - Number
MDN - parseInt
MDN - parseFloat


#3

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