Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to create a code which asks the user to input two numbers. The program needs to multiply the two numbers and then display the answer in a displayed message. ie: 5 x 7 is 35! (assuming the numbers the user inputed were 5 and 7.)
Here is the code I have now.
<title></title>
<script type="text/javascript">
var num1 = 0;
var num2 = 0;
var calculatedNum = 0;
function calculation() {
//create a integer variable called num2
//Purpose: second variable
var num2 = 0; // integer second variable
//create a integer variable called calculatedNum
//Purpose: num1 x num2
var calculatedNum = 0; // integer num1 x num2
//ask user '"Please type the first number"'
//and put the answer in num1
num1 = prompt("Please type the first number");
//ask user '"Please type the second number."'
//and put the answer in num2
num2 = prompt("Please type the second number.");
//Tell user: num1 + "x" + num2 + "=" + calculatedNum
alert (num1 + "x" + num2 + "=" + calculatedNum);
} // end calculation function
}end program
</script>
Try it
var num1 = 0;
var num2 = 0;
var calculatedNum = 0;
function calculation() {
num1 = prompt("Please type the first number");
num2 = prompt("Please type the second number.");
calculatedNum = (num1*num2);
alert(num1 + "x" + num2 + "=" + calculatedNum);
}
use calculation(); to call :)
You've overcomplicated things for yourself. Just do this:
<script>
var num1 = prompt('Please type the first number'),
num2 = prompt('Please type the second number');
alert(num1 + "x" + num2 + "=" + num1 * num2);
</script>
Demo 1
Or, if you want to wrap this in a function so you can call it from somewhere else:
<script>
function calculation() {
var num1 = prompt('Please type the first number'),
num2 = prompt('Please type the second number');
alert(num1 + "x" + num2 + "=" + num1 * num2);
};
calculation();
</script>
Demo 2
<title></title>
<script type="text/javascript">
var num1 = 0;
var num2 = 0;
var calculatedNum = 0;
function calculation() {
//create a integer variable called num2
//Purpose: second variable
var num2 = 0; // integer second variable
//create a integer variable called calculatedNum
//Purpose: num1 x num2
var calculatedNum = 0; // integer num1 x num2
//ask user '"Please type the first number"'
//and put the answer in num1
num1 = prompt("Please type the first number");
//ask user '"Please type the second number."'
//and put the answer in num2
num2 = prompt("Please type the second number.");
calculatedNum = num1*num2;
//Tell user: num1 + "x" + num2 + "=" + calculatedNum
alert (num1 + "x" + num2 + "=" + calculatedNum);
} // end calculation function
calculation();
</script>
This works as intended.
you were having an extra "}end program" at the end.
Related
I assigned three cells of numbers and then I wanted to sum them together,
var num1, num2, num3: number;
num1 = prompt("Enter num1:");
num2 = prompt("Enter num2:");
num3 = prompt("Enter num3:");
document.write((num1 + num2 + num3));
alert((num1 + num2 + num3));
and if i enter 10 , 20 , 30 the out put is : 102030 ,
and its need to be 60...
Thanks for the helpers.
The prompt() function gives you a string so you have to convert it a number.
You can do what you want this way:
var num1, num2, num3;
num1 = parseInt(prompt("Enter num1:"));
num2 = parseInt(prompt("Enter num2:"));
num3 = parseInt(prompt("Enter num3:"));
document.write((num1 + num2 + num3));
alert((num1 + num2 + num3));
If you wish to use floats instead, simply replace parseInt with parseFloat.
The prompt() function work with string Values,
If you want to return type number add a (+) after the (=),
Take a look at this code and understand better what I meant :
let testPrompt1;
let testPrompt2;
testPrompt1 = prompt("Enter test 1:"); // Enter number
testPrompt2 = + prompt("Enter test 2:"); // Enter number
document.write("Type of test 1" + typeof testPrompt1 ); // String !!
document.write("Type of test 2" + typeof testPrompt2 ); // Number !!
// Add (+) before the (=) !!!!
let num1, num2, num3;
num1 =+ prompt("Enter num1:"); // 10
num2 = + prompt("Enter num2:");// 20
num3 = + prompt("Enter num3:");// 30
document.write((num1 + num2 + num3)); // Output 60
alert((num1 + num2 + num3)); // Output 60
I also suggest adding a variable of the result and not printing everything in one line for a better understanding of the code.
let num1, num2, num3, res;
num1 = + prompt("Enter num1:");
num2 = + prompt("Enter num2:");
num3 = + prompt("Enter num3:");
res = num1 + num2 + num3;
document.write("Result : " + res); // Output 60
alert("Result : " + res); // Output 60
Make sure you convert them from string to number:
alert((Number(num1 ) + Number(num2) + Number(num3));
JS is treating numbers like strings now, so it concatinates them. Try cast them to int(integers) so they can be added.Write smth like:
document.write(Number(num1) + Number(num2) + Number(num3)))
alert(Number(num1)+Number(num2)+Number(num3))
This question already has answers here:
Why is parseInt() not working? [duplicate]
(4 answers)
Closed 3 years ago.
Here's my code:
function add() {
var num1 = prompt("Enter 1st number")
var num2 = prompt("Enter 2nd number")
parseInt(num1);
parseInt(num2);
var result = num1 + num2;
alert(result);
}
add();
I'm trying to build a simple addition calculator. parseInt wasn't working and an answer here said to declare the variable like var num1 = parseInt(num1);. However since I'm only getting "num1" through user input (var num1 = prompt..."), not sure how to store it as integer, or parseInt, in the same line. Any advice? Thanks.
All you have here are standalone, unused expressions:
parseInt(num1);
parseInt(num2);
Those evaluate to numbers, but you don't use them, so they're useless. Either assign them to variables, eg
const actualNum1 = parseInt(num1);
const actualNum2 = parseInt(num2);
and then use those variables, or just wrap the prompt in parseInt:
var num1 = parseInt(prompt("Enter 1st number"))
var num2 = parseInt(prompt("Enter 2nd number"))
Unless you're intending to only accept integers, consider using Number instead:
var num1 = Number(prompt("Enter 1st number"))
var num2 = Number(prompt("Enter 2nd number"))
Im trying to accept 2 inputs from a user then compare the 2 to find the smallest number.
Finally print to the console. I feel Im going in the wrong direction.
Any advice?
Below is my code
//prompt variable for user input
let num1 = prompt("Enter 1st number ", "i.e. 7 ");
let num2 = prompt("Enter 2nd number ", "i.e. 4 ");
// for loop returning lowest input
for (let i = 1; i < num1.length; i++){
if (num1[i] <= num2){
num2 = num1[i];
}
}
console.log(num2);
If there's only 2 inputs and you're trying to console.log the smallest number then you can just use an if statement. No need to iterate through num1.
let num1 = 5;
let num2 = 10;
if(num1 > num2){
console.log(num2);
}else if (num2 > num1){
console.log(num1);
}else{
console.log(num1 + ' and '+num2+' are equal');
}
I have to make a game of dice where I add the values of both the dice and give the sum. You get another turn if both the dice end up on the same number (such as 2 and 2 which is 4) then you roll the dice again and the 2 new numbers get added to the previous sum (like 4 mentioned earlier).
For example -
1st try 3 and 3, sum is 6.
2nd try 4 and 5, sum is 6 + 4 + 5 = 15
I'm able to get the first sum right, but the subsequent numbers are getting messed up sometimes even giving twice as much.
function oneRandom(){
var my1 = Math.floor(Math.random() * 6 ) +1 ;
var my2 = Math.floor(Math.random() * 6 ) +1 ;
var num1 = Number(my1);
var num2 = Number(my2);
var sum = num1 + num2;
console.log(sum);
var resultfield = document.getElementById("total").innerHTML = "<h1>"+sum+"</h1>";
document.getElementById("cilc").style.display = "none";
if (getImage.src == get2.src){
document.getElementById("cilc").style.display = "block";
var sum2 = num1 + num2;
var total = sum2 + sum;
var resultfield = document.getElementById("total").innerHTML = "<h1>"+total+"</h1>";
}
}
The getImage and get2 are arrays to match dice images to give another turn in case of same numbers loading.
You can try this,
function rollDice (times) {
var num1 = Math.floor(Math.random() * Math.floor(6));
var num2 = Math.floor(Math.random() * Math.floor(6));
console.log(times, num1, num2);
var total = num1 + num2;
return num1 === num2 ? total + rollDice(times + 1) : total;
}
console.log(`result is ${rollDice(0)}`);
I'm trying to write a script on a webpage that presents the user with an arithmetic problem (EG what is 2 + -4) and asks for the answer using a prompt. then give them the feedback using an alert box. the integers have to be between -10 and 10 so far I've tried this and no luck:
var num = Math.Floor((Math.random() * -10) + 10);
var numPrompt = prompt("what is " + (num + num));
alert(numPrompt);
then I tried:
var num = Math.Floor((Math.random() * -10) + 10);
var numPrompt = prompt( "what is " + (parseInt(num) + parseInt(num)) );
alert(numPrompt);
both failed, but why?
Here's something similar to what you want:
var num1 = Math.floor((Math.random() * -21) + 11);
var num2 = Math.floor((Math.random() * -21) + 11);
var userAnswer = prompt('What is ' + num1 + ' + ' + num2 + '?');
if (userAnswer.trim() !== '' && +userAnswer === num1 + num2) {
alert('Correct');
} else {
alert('Wrong');
}
Since num1 and num2 are numbers being generated by the Javascript, we can simply compare the number +userAnswer to num1 + num2.
The reason for doing +userAnswer is the userAnswer variable contains the answer returned by the prompt function, which is a string, so putting a + sign infront of it converts it to a number.
I like +userAnswer more than parseInt because parseInt('5a') returns 5 where as +'5a' returns 0.
The only we have to watchout for is +'' or +' ' returns 0 so we have to do an additional check to make sure the user didn't just press enter without typing a number.