Unexpected output in javascript

后端 未结 5 651
旧时难觅i
旧时难觅i 2020-12-12 08:38

I am beginner to javascript and i am getting unexpected output

here is the code



        
5条回答
  •  暖寄归人
    2020-12-12 09:01

    This is because the prompt function returns a String and not a Number. So what you're actually doing is to request 2 strings and then concatenate them. If you want to add the two numbers together you'll have to convert the strings to numbers:

    var num1 = parseFloat(prompt("what is your no."));
    var num2 = parseFloat(prompt("what is another no."));
    

    or simpler:

    var num1 = +prompt("what is your no.");
    var num2 = +prompt("what is another no.");
    

提交回复
热议问题