cannot use + when try to add number

后端 未结 4 1432
日久生厌
日久生厌 2020-11-30 15:06

After i selected option 1 and after i add the number like 100 it results like this 100100

This is the code:

var userAmount;
    var userMoney = 100;
         


        
4条回答
  •  一向
    一向 (楼主)
    2020-11-30 15:16

    A common mistake for people new to JavaScript is they forget almost every value taken from user input is a String. You'll need to convert these into numbers before you can use the addition + operator. This conversion can be done with the unary + or parseFloat, for example

    userAmount = parseFloat(prompt("Amount: "));
    

    When using Strings in JavaScript, the + operator is actually string concatenation

提交回复
热议问题