How do I sum numbers using a prompt, like a simple calculator? [duplicate]

雨燕双飞 提交于 2019-12-02 09:40:28

Yours doesn't work because it's effectively doing "15" + 100 = 15100 because prompt returns a string.

You need to cast it from a string to a number using parseInt

var num1 = parseInt(prompt("Input a number."), 10) //10 for decimal
var num2 = 100
alert(num1 + num2)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!