Javascript User-input for operators not outputing the integers - javascript

I'm supposed to take a few numbers and give them out in a list prompt.
EDIT, SOLVED thank you

concatenate the variables with the operators like you did with "Num 1= " + num1 , and you can do the math directly in the document.write without reassigning result each time :
var entry;
var average;
var total = 0;
entry = prompt("enter first number");
entry = parseInt(entry);
var num1 = entry;
total = total + num1;
entry = prompt("enter second number");
entry = parseInt(entry);
var num2 = entry;
total = total + num2;
average = parseInt(total / 2);
document.write("<h2>Arithmetic and operators</h2>")
document.write('Num 1 = ' + num1 + ' <br> Num 2 = ' + num2 + ' <br> Average num = ' + average + '<br>')
document.write(num1 + ' + ' + num2 + ' = ' + ( num1 + num2 ));
document.write("<br>")
document.write(num1 + ' - ' + num2 + ' = ' + ( num1 - num2 ));
document.write("<br>")
document.write(num1 + ' / ' + num2 + ' = ' + ( num1 / num2 ));
document.write("<br>")
document.write(num1 + ' % ' + num2 + ' = ' + ( num1 % num2 ));
document.write("<br>")
document.write(num1 + ' * ' + num2 + ' = ' + ( num1 * num2 ));
document.write("<br>")

Related

only final output is being displayed

I'm trying to make a program to roll three random dice with n number of sides (determined by user input), and then continue rolling the dice and showing the random numbers until all three numbers are the same.
I think my logic is fine, but the page only shows the final row where all the numbers are the same and not the ones prior to it (where they are not all the same).
{
let sidesNumber = parseInt(prompt("Enter the number of sides for your dice:", ""));
let output1 = -1;
let output2 = -2;
let output3 = -3;
while( output1 != output2 || output1 != output3 )
{
output1 = Math.floor((Math.random()* sidesNumber + 1));
output2 = Math.floor((Math.random()* sidesNumber + 1));
output3 = Math.floor((Math.random()* sidesNumber + 1));
document.getElementById("result").innerHTML = output1 + " " + output2 + " " + output3;
}
}
The reason this is happening is you are not appending output1, output2, and output3 the Math.floor((Math.random()* sidesNumber + 1)), you are redefining the whole variable.
For example,
let output1 = -1;
let output2 = -2;
let output3 = -3;
output1 = Math.floor((Math.random()* sidesNumber + 1));
output2 = Math.floor((Math.random()* sidesNumber + 1));
output3 = Math.floor((Math.random()* sidesNumber + 1));
The output varaible is -1, -2, etc.
So to append the variables as Math.floor((Math.random()* sidesNumber + 1)), use += instead of =.
I've also realized document.getElementById("result").innerHTML = should be document.getElementById("result").innerHTML +=. I fixed that too.
{
let sidesNumber = parseInt(prompt("Enter the number of sides for your dice:", ""));
let output1 = -1;
let output2 = -2;
let output3 = -3;
while( output1 != output2 || output1 != output3 )
{
output1 += Math.floor((Math.random()* sidesNumber + 1));
output2 += Math.floor((Math.random()* sidesNumber + 1));
output3 += Math.floor((Math.random()* sidesNumber + 1));
document.getElementById("result").innerHTML += output1 + " " + output2 + " " + output3;
}
}
You're very almost there.
Currently, instead of displaying each roll on the screen, your script rolls the dice invisibly in the background and the roll is displayed only when the desired result occurs.
To display each roll, swap the line:
document.getElementById("result").innerHTML = output1 + " " + output2 + " " + output3;
for something like:
document.body.innerHTML += '<p>' + output1 + " " + output2 + " " + output3 + '</p>';
Working Example:
{
let sidesNumber = parseInt(prompt("Enter the number of sides for your dice:", ""));
let output1 = -1;
let output2 = -2;
let output3 = -3;
while( output1 != output2 || output1 != output3 )
{
output1 = Math.floor((Math.random()* sidesNumber + 1));
output2 = Math.floor((Math.random()* sidesNumber + 1));
output3 = Math.floor((Math.random()* sidesNumber + 1));
document.body.innerHTML += '<p>' + output1 + " " + output2 + " " + output3 + '</p>';
}
}
Here is a working example storing the values inside a ul element
{
let sidesNumber = parseInt(prompt("Enter the number of sides for your dice:", ""));
let output1 = -1;
let output2 = -2;
let output3 = -3;
while( output1 != output2 || output1 != output3 )
{
output1 = Math.floor((Math.random()* sidesNumber + 1));
output2 = Math.floor((Math.random()* sidesNumber + 1));
output3 = Math.floor((Math.random()* sidesNumber + 1));
document.getElementById("result").innerHTML += "<li>" + output1 + " " + output2 + " " + output3 + "</li>";
}
}
<ul id="result"></ul>
Note that every time you run your code you override the value of the innerHTML with =, you just have to fix it by writing +=
document.getElementById("result").innerHTML += output1 + " " + output2 + " " + output3;

if statement displaying in text box without executing calculation

I'm new at this. The input is showing in the text box but not making the calculation for the if statement.
$w.onReady(function () {
$w("#generatequote").onClick((event) => {
var SR = Number($w("#SR").value);
if (SR<100) {
$w("#quotetext").value = SR * 2;
}
//first try- does not calculate
$w("#quotetext").value = fin + "\n" + (name + "\n" + email + "\n" + phonenumber + "\n" + address + "\n"
+ ($w("#quotetext").value = SR))
//Second Try- does not calculate
$w("#quotetext").value = fin + "\n" + (name + "\n" + email + "\n" + phonenumber + "\n" + address + "\n"
+ ($w("#SR").value = SR))
I have also tried replacing "#quotetext" in the if statement with "#SR" but it displays nothing
This is the code displaying the additional else statements
var SR = Number($w("#SR").value);
if (SR<100) {
$w("#quotetext").value = SR * 2;
}
else if (SR>=100&&SR<300) {
$w("#quotetext").value = SR * 1.5;
}
else if (SR>=300&&SR<600) {
$w("#quotetext").value * 1.25;
}
else if(SR>=600) {
$w("#SR").value = ("SR");
}
$w("#quotetext").value = fin + "\n" + (name + "\n" + email + "\n" + phonenumber + "\n" + address + "\n" + ($w("#SR").value = SR))
You are reassigning your $w("#quotetext").value after the if statement ends. Either put the next code in else blocks or the previous outputs won't be shown as they will get replaced by later ones.
Just don't reassign the $w("#quotetext").value after your if statements end, or use a variable in your if statements instead of using $w("#quotetext").value
var SR = Number($w("#SR").value);
if (SR<100) {
SR = SR * 2;
}
else if (SR>=100&&SR<300) {
SR = SR * 1.5;
}
else if (SR>=300&&SR<600) {
SR= SR * 1.25;
}
else if(SR>=600) {
SR = ("SR");
}
$w("#quotetext").value = fin + "\n" + (name + "\n" + email + "\n" + phonenumber + "\n" + address + "\n" + (SR))

User enters 2 numbers, output numbers and sums in table. Outputting, but not in table?

Write a program that asks a user to enter three numbers. If all three entries are numbers then output the sum of all combinations of pairs of numbers from the three entries. The code that performs the addition and outputs the result for each pair should be in a function and is called from the main function. Output the answers in an HTML table. The table and its end tag should be added to the HTML page. The table rows will be inserted with the program.
I can get the program to mostly work, but it's not outputting the numbers and sums as part of the table, and I'm not sure where/what I'm missing?
//function to output sum of number1 and number2
function outputSumOfnumber1Andnumber2(number1, number2) {
var output;
sum1and2 = number1 + number2;
output = document.getElementById('outputPart2');
output.innerHTML += "<tr><td>" + number1 + "</td><td>" + number2 + "</td><td>" + sum1and2 + "</td></tr>";
}
function outputSumofnumber2Andnumber3(number2, number3) {
var output;
sum2and3 = number2 + number3;
output = document.getElementById('outputPart2');
output.innerHTML += "<tr><td>" + number2 + "</td><td>" + number3 + "</td><td>" + sum2and3 + "</td></tr>";
}
function outputSumofnumber1Andnumber3(number1, number3) {
var output;
sum1and3 = number1 + number3;
output = document.getElementById('outputPart2');
output.innerHTML += "<tr><td>" + number1 + "</td><td>" + number3 + "</td><td>" + sum1and3 + "</td></tr></table>";
}
function exercise3Part2() {
// PART 2: YOUR CODE STARTS AFTER THIS LINE
var number1;
var number2;
var number3;
var output;
var tableRows;
number1 = Number(prompt("Enter a number:"));
number2 = Number(prompt("Enter another number:"));
number3 = Number(prompt("Enter one more number:"));
output = document.getElementById('outputPart2');
output.innerHTML = "<table><tr><th>Pair part 1</th><th>Pair part 2</th><th>Sum</th></tr>";
outputSumOfnumber1Andnumber2(number1, number2);
outputSumofnumber2Andnumber3(number2, number3);
outputSumofnumber1Andnumber3(number1, number3);
}
Attempt 2--
//function to output sum of numbers
function outputSumOfnumbers(number1, number2, number3, rows) {
var output;
var rows;
sum1and2 = number1 + number2;
sum2and3 = number2 + number3;
sum1and3 = number1 + number3;
output = document.getElementById('outputPart2');
rows = "<tr><td>" + number1 + "</td><td>" + number2 + "</td><td>" + sum1and2 + "</td></tr>";
rows += "<tr><td>" + number2 + "</td><td>" + number3 + "</td><td>" + sum2and3 + "</td></tr>";
rows += "<tr><td>" + number1 + "</td><td>" + number3 + "</td><td>" + sum1and3 + "</td></tr>";
}
function exercise3Part2() {
// PART 2: YOUR CODE STARTS AFTER THIS LINE
var number1;
var number2;
var number3;
var myTable;
var rows;
myTable = document.getElementById('outputPart2');
number1 = Number(prompt("Enter a number:"));
number2 = Number(prompt("Enter another number:"));
number3 = Number(prompt("Enter one more number:"));
outputSumOfnumbers(number1, number2, number3);
myTable = document.getElementById('outputPart2');
myTable.innerHTML += rows;
}
Well, you messed things up, but instead of fixing this code, I prefer that you choose a better approach on table manipulation, this is definitely NOT the way you should do it.
Check this article: http://www.htmlgoodies.com/beyond/css/working_w_tables_using_jquery.html
I believe that you will get it right when you apply what is inside. (:
I added an opening and closing table tag to my HTML (with an id) and used the below code:
//function to output sum of number1 and number2
function outputSumOfnumber1Andnumber2(number1, number2) {
var output;
sum1and2 = number1 + number2;
output = document.getElementById('outputPart2');
output.innerHTML += "<tr><td>" + number1 + "</td><td>" + number2 + "</td><td>" + sum1and2 + "</td></tr>";
}
function outputSumofnumber2Andnumber3(number2, number3) {
var output;
sum2and3 = number2 + number3;
output = document.getElementById('outputPart2');
output.innerHTML += "<tr><td>" + number2 + "</td><td>" + number3 + "</td><td>" + sum2and3 + "</td></tr>";
}
function outputSumofnumber1Andnumber3(number1, number3) {
var output;
sum1and3 = number1 + number3;
output = document.getElementById('outputPart2');
output.innerHTML += "<tr><td>" + number1 + "</td><td>" + number3 + "</td><td>" + sum1and3 + "</td></tr></table>";
}
function exercise3Part2() {
// PART 2: YOUR CODE STARTS AFTER THIS LINE
var number1;
var number2;
var number3;
var output;
var tableRows;
number1 = Number(prompt("Enter a number:"));
number2 = Number(prompt("Enter another number:"));
number3 = Number(prompt("Enter one more number:"));
output = document.getElementById('outputPart2');
output.innerHTML = "<tr><th>Pair part 1</th><th>Pair part 2</th><th>Sum</th></tr>";
outputSumOfnumber1Andnumber2(number1, number2);
outputSumofnumber2Andnumber3(number2, number3);
outputSumofnumber1Andnumber3(number1, number3);
}

Random number maker

I have a problem with my code . I want to make a program that generates random numbers and multiplies them by 50 then adds 30 to them then removes the remaining decimals.
I also want to show how these number are calculated by using <h1> tags.
after the equal symbol I want to show the final number that is multiplied by 50 and added by 30 and the decimals are removed.
<html>
<head>
<meta charset="UTF-8"/>
<title>Random Numbers</title>
<script>
function start(){
var num1 = Math.random();
var num2 = Math.random();
var num3 = Math.random();
var num4 = Math.random();
var num5 = Math.random();
var num11 = num1.toFixed(3);
var num22 = num2.toFixed(3);
var num33 = num3.toFixed(3);
var num44 = num4.toFixed(3);
var num55 = num5.toFixed(3);
/*var num111 = (num11 * 50);
var num222 = (num22 * 50);
var num333 = (num33 * 50);
var num444 = (num44 * 50);
var num555 = (num55 * 50);
/* ----------------------------------------------
var num111 = (num11) += 30;
var num222 = (num22) += 30;
var num333 = (num33) += 30;
var num444 = (num44) += 30;
var num555 = (num55) += 30;*/
document.getElementById("show1").innerHTML=num11 + '×' + '50' + '+' + '30' + '=' + num11;
document.getElementById("show2").innerHTML=num22 + '×' + '50' + '+' + '30' + '=' + num22;
document.getElementById("show3").innerHTML=num33 + '×' + '50' + '+' + '30' + '=' + num33;
document.getElementById("show4").innerHTML=num44 + '×' + '50' + '+' + '30' + '=' + num44;
document.getElementById("show5").innerHTML=num55 + '×' + '50' + '+' + '30' + '=' + num55;
}
</script>
</head>
<body>
<button id="btn" onclick="start()">Press the button</button><br/>
<h1 id="show1"></h1>
<h1 id="show2"></h1>
<h1 id="show3"></h1>
<h1 id="show4"></h1>
<h1 id="show5"></h1>
</body>
Check this: http://jsfiddle.net/wc5wnby5/2/
Probably you need to do like below:
document.getElementById("show1").innerHTML=num11 + '×' + '50' + '+' + '30' + '=' + Math.round(num11*50+30);
document.getElementById("show2").innerHTML=num22 + '×' + '50' + '+' + '30' + '=' + Math.round(num22*50+30);
document.getElementById("show3").innerHTML=num33 + '×' + '50' + '+' + '30' + '=' + Math.round(num33*50+30);
document.getElementById("show4").innerHTML=num44 + '×' + '50' + '+' + '30' + '=' + (num44*50+30);
document.getElementById("show5").innerHTML=num55 + '×' + '50' + '+' + '30' + '=' + Math.round(num55*50+30);
Use Math.round() for removing decimal part. Consider following code:
document.getElementById("show1").innerHTML=num11 + '×' + '50' + '+' + '30' + '=' + Math.round(num11*50+30);
document.getElementById("show2").innerHTML=num22 + '×' + '50' + '+' + '30' + '=' + Math.round(num22*50+30);
document.getElementById("show3").innerHTML=num33 + '×' + '50' + '+' + '30' + '=' + Math.round(num33*50+30);
document.getElementById("show4").innerHTML=num44 + '×' + '50' + '+' + '30' + '=' + Math.round(num44*50+30);
document.getElementById("show5").innerHTML=num55 + '×' + '50' + '+' + '30' + '=' + Math.round(num55*50+30);
DEMO
Reference for more details.
If i right understood your problem:
function print(elemID, text){
document.getElementById(elemID).appendChild(document.createTextNode(text));
}
var res;
print('show1', (res = Math.random()));
print('show2', (res = res * 50));
print('show3', (res += 30));
print('show4', (res = res.toFixed(0)));
fiddle

Prompt() method in Javascript

I'm a beginner to Javascript and I have a basic question about how to use the prompt method. None of the code seems to process below. Is there some sort of hidden rule about using multiple prompt boxes or does my code just have a syntax error? Any help would be much appreciated. Thanks in advance.
<html>
<head>
<title> Two Numbers </title>
<script type="text/javascript">
var first = prompt("Enter first number:");
var second = prompt("Enter second number:");
var sum = (first-0) + (second-0);
var diff = first - second;
var divide = first/second;
var multi = first*second;
document.write(first + " + " + second " = " + sum + "<br />");
document.write(first + " + " + second " = " + diff + "<br />");
document.write(first + " + " + second " = " + divide + "<br />");
document.write(first + " + " + second " = " + multi + "<br />");
</script>
</head>
<body>
</body>
</html>
You're missing a +.
//change this
console.log(first + " + " + second " = " + sum + "<br />");
// to this
console.log(first + " + " + second + " = " + sum + "<br />");
In the future, please use the console for debugging. There is a great article on everything you can do with the console here > https://developer.chrome.com/devtools/docs/javascript-debugging
Corrected the syntax error and corrected the operators in the write() function:
<html>
<head>
<title> Two Numbers </title>
<script type="text/javascript">
var first = prompt("Enter first number:");
var second = prompt("Enter second number:");
var sum = (first-0) + (second-0);
var diff = first - second;
var divide = first/second;
var multi = first*second;
document.write(first + " + " + second + " = " + sum + "<br />");
document.write(first + " - " + second + " = " + diff + "<br />");
document.write(first + " / " + second + " = " + divide + "<br />");
document.write(first + " * " + second + " = " + multi + "<br />");
</script>
</head>
<body>
</body>
</html>
use the console to check for errors, as said by James G
var isValid = true;
var first = prompt("Enter first number:");
if (!Number(first)) {
alert("Please enter numeric value only.");
isValid = false;
}
if (isValid) {
var second = prompt("Enter second number:");
if (!Number(second)) {
alert("Please enter numeric value only.");
isValid = false;
}
if (isValid) {
var sum = first + second;
var diff = first - second;
var divide = first / second;
var multi = first * second;
console.log(first + " + " + second + " = " + sum);
console.log(first + " - " + second + " = " + diff)
console.log(first + " / " + second + " = " + divide);
console.log(first + " * " + second + " = " + multi);
}
}

Categories