Sum of two numbers with prompt

后端 未结 8 2447
甜味超标
甜味超标 2020-11-28 14:47

I\'ve been trying to solve this problem for the last couple days: when I subtract, multiply or divide 2 numbers input through a prompt, everything works fine; but when I wan

相关标签:
8条回答
  • 2020-11-28 15:36

    try this ,

    var a = prompt("Enter first number");
    var b = prompt("Enter second number");
    var x=parseInt(a);
    var y=parseInt(b);
    
    alert(x+y);
    
    0 讨论(0)
  • 2020-11-28 15:41

    When you enter var a and var b, the variables are probably set strings(characters) and not as integers.

    So, when you use a + b, your'e putting those characters together.

    For ensuring that the value you enter saves as an integer, you can you use the

    parseInt()
    

    For example: var a = parseInt( prompt("Enter first number") );

    0 讨论(0)
提交回复
热议问题