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))
Related
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)}`);
Can somebody help me with this. Here I have a set of 3 different numbers as strings that I get from a user and I also have another 3 randomly generated numbers that I want to compare with the set of numbers that I get from a users but since I'm not gonna do any calculations with the number I didn't see any reason to convert them so I'm trying to compare them as strings
function radGenerator(userno1,userno2,userno3,wnum1, wnum2, wnum3,dvresult) {
$(wnum1).text("" + Math.floor((Math.random() * 10) + 1));
$(wnum2).text("" + Math.floor((Math.random() * 10) + 1));
$(wnum3).text("" + Math.floor((Math.random() * 10) + 1));
var num1 = num2 = num3 = "";
if($(userno1).text()== $(wnum1).text()||
$(userno1).text()== $(wnum2).text()||
$(userno1).text()== $(wnum3).text()){
num1 = $(userno1).text();
}
if($(userno2).text()== $(wnum1).text()||
$(userno2).text()== $(wnum2).text()||
$(userno2).text()== $(wnum3).text()){
num2 = $(userno2).text();
}
if($(userno3).text()== $(wnum1).text()||
$(userno3).text()== $(wnum2).text()||
$(userno3).text()== $(wnum3).text()){
num3 = $(userno3).text();
}
$(dvresult).text("Winning Numbers: " + num1 +" "+ " "+ num2 + " "+ num3);
}
radGenerator("#uans1","#uans2","#uans3","#wno1", "#wno2", "#wno3","#divresult");
In order to compare strings in JavaScript, you should the localeCompare() function. "string".localeCompare("otherString"); will return 1 because "string" comes after "otherString". The function would return 0 if they are equal and -1 if the first string comes before the string you are comparing it to.
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.
I calculate a lot of number with javascript. In some calculation i have a bad result.
Exemple: http://jsfiddle.net/6et8d10c/
var num1 = 2.01;
var num2 = 22.29;
var result = num1 + num2; //24.29 (should be 24.30)
For now I fix with:
(2.01*100 + 22.29*100) / 100 = 24.30
That's not perfect. Why can't i calculate 2.01 + 22.29 ?
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.