Calculation | Sololearn: Learn to code for FREE!



I am trying to add totalAmount + tipAmount, but the code is treating the variables as a string rather than as a numerical value. How would I correct this? Ie: 100 + 20 currently = 10020, but I want it to show 120 var totalAmount = prompt ("Enter total amount billed") var tipAmount = prompt ("Enter tip amount") document.write (totalAmount + tipAmount);

11/7/2019 8:35:43 AM


3 Answers

New Answer


A. Quick answer parseInt(var) or parseFloat(var) or Number(var) B. demo C. Explanation the + operator can be Number.addition or String.concantenation. D. There is a difference between parseInt and Number, read:


Here in this case, you can use parseInt which is an inbuilt function in JavaScript which converts a String into an integer value. You can read here more about it : After taking prompt input you can convert it into integer as it take integer input. totalAmount = parseInt(totalAmount); Same applies for tipAmount.


thank you!