How to use numbers taken from prompt() and use in calculation?
The prompt()
method only returns a string.
You have to convert the string to a number using these methods:
Number()
parseInt()
parseFloat()
Generally you can use Number()
method.
Add a sum
variable and store the sum result of the numbers to it. The code is given below:
var numOne = prompt('Give me a number');
var numTwo = prompt('Give me another number');
var sum = Number(numOne) + Number(numTwo);
alert('The sum of your numbers is ' + sum);
How to write a javaScript function to enter 2 numbers through prompts and display the sum of those numbers in an alert?
To do it with only one prompt:
var numbers = prompt("Enter two numbers separated by a semi-colon: ", "");
var a = numbers.split(";")[0];
var b = numbers.split(";")[1];
function calc(a,b)
{
return parseFloat(a) + parseFloat(b);
}
alert ("The addition of " + numbers + " is = " + calc(a,b));
adding from the window object prompt
So the issue I guess you are facing is that when you add the inputs (eg: 3, 2) you get 32 instead of 5. This happens because the input retrieved by prompt()
is a string and it needs to be converted/typecast into a number. You can do this by the Number(input)
or parseInt(input, "10")
functions.
alert(total = " You entered " + userInput1 + " + " + userInput2 + " which equals " + (Number(userInput1) + Number(userInput2)));
Sum of two numbers and the numbers between them
// prompt user to enter two numbersvar number1 = prompt("Enter first number: ");var number2 = prompt("Enter a number bigger than first number: ");
// convert user input into numbersvar number1 = Number(number1);var number2 = Number(number2);
var start_point= number1;var sum=0;
// display number1document.write(start_point);sum += start_point;start_point++;
while (start_point <=number2) { document.write(" + " + start_point); sum += start_point; start_point++;}
// display sumdocument.write(" = " + sum);
function add() numbers with prompt
Your code is more complicated than it need be. There is no need for a nested function to do the math. You must also convert the user-supplied data to a number (this can be done by prepending a +
to the data).
Also, your code does not follow best-practices and standards.
See the comments in the code below.
// Get references to the HTML elements you'll use in JavaScriptvar btn = document.getElementById("btn");var output = document.getElementById("demo");var counter = 0; // Running total will be here
// Set up event handling in JavaScript, not HTMLbtn.addEventListener("click", function() { counter += +prompt("What is the number to add?"); // Convert response to number and add to counter output.textContent = counter; // Place result back into the document (use .textContent, not .innerHTML)});
<button id="btn">+</button><p id="demo">0</p>
Related Topics
How to Remove Element from an Array in JavaScript
How to Highlight the Text of the Dom Range Object
Is a JavaScript Array Index a String or an Integer
Var Name Produces Strange Result in JavaScript
How to Clone a JavaScript Object Except for One Key
How to Add a Key Prop to a React Fragment
Split String Only on First Instance of Specified Character
Fulfill (Don't Resolve) Promise with Another Promise
How to Create a Web Worker from a String
JavaScript Change Date into Format of (Dd/Mm/Yyyy)
How to Print a Stack Trace in Node.Js
How to Check Whether an Object Is a Date
How to Require a Controller in an Angularjs Directive
Convert Js Object to Form Data
Calculate Date from Week Number in JavaScript
Invariant Violation: _Registercomponent(...): Target Container Is Not a Dom Element