我要求用户输入第一个电话号码。然后,我要求提供比第一个更大的数字。将所有数字从第一个数字乘以第二个,然后输出结果。
我将其作为while循环工作,但需要将其切换为for循环。当我尝试将其切换为for循环时,我不断得到错误的乘法值。不知道需要做什么。
const INITIAL_VALUE = 0;
// declare variables
var number1;
var number2;
var multiply;
var middlePoint;
// initialize sum
multiply = INITIAL_VALUE;
// prompt user to enter two numbers
number1 = prompt("Enter first number: ");
number2 = prompt("Enter a number bigger than first number: ");
// convert user input into numbers
number1 = Number(number1);
number2 = Number(number2);
// assign middlePoint
middlePoint = number1;
// display number1
document.write(number1);
// assign multiply and increment middlePoint
multiply = middlePoint;
middlePoint++;
// while loop to display numbers in between and multiply
//**NEEDS TO BE A FOR LOOP, NOT A WHILE LOOP**
while (middlePoint < number2) {
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;
}
// add multiply to number2 for final multiplication total
multiply *= number2;
// display number2 and final multiplication total
document.write(" * " + number2 + " = " + multiply);
答案 0 :(得分:0)
欢迎使用StackOverflow!
要切换到for
循环,只需确保循环数与while
循环相同即可。在这种情况下,要进行的循环数为number2 - number1 - 1
或number2
和number1
之间的数量。因此,您的for
循环将是:
for (var i=0; i<number2-number1-1; i++) {
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;
}
答案 1 :(得分:0)
当前,您的代码编写为经过while循环。在检查布尔条件时,过渡到for循环比使用while循环更麻烦。但是,如果您真的想要for循环,请执行以下操作:
for (;middlePoint < number2;) {
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;
}
答案 2 :(得分:0)
您可以使用以下代码将所有数字从n1乘以n2:mul
将包含乘法结果:
var mul = 1;
var firstNumber = 0;
var secondNUmber = 0;
var firstNumberStr = '';
var secondNUmberStr = '';
// prompt user to enter two numbers
firstNumberStr = prompt("Enter first number: ");
while(isNaN(firstNumberStr)){
firstNumberStr = prompt("Enter Correct first number: ");
}
secondNUmberStr = prompt("Enter a number bigger than first number: ");
while(isNaN(secondNUmberStr) || parseInt(firstNumberStr) >= parseInt(secondNUmberStr)){
secondNUmberStr = prompt("Enter a number bigger than first number: ");
}
firstNumber = parseInt(firstNumberStr);
secondNUmber = parseInt(secondNUmberStr);
for(;firstNumber <= secondNUmber;){
mul *= firstNumber;
firstNumber++
}
alert(mul);
答案 3 :(得分:0)
您的代码可以如下修改:
// declare variables
var number1;
var number2;
var multiply;
var middlePoint;
const INITIAL_VALUE = 0;
// initialize sum
multiply = INITIAL_VALUE;
// prompt user to enter two numbers
number1 = prompt("Enter first number: ");
number2 = prompt("Enter a number bigger than first number: ");
// convert user input into numbers
number1 = Number(number1);
number2 = Number(number2);
// assign middlePoint
middlePoint = number1;
// display number1
document.write(number1);
// assign multiply and increment middlePoint
multiply = middlePoint;
middlePoint++;
// while loop to display numbers in between and multiply
for (var i=0;middlePoint < number2; i++) {
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;
}
// add multiply to number2 for final multiplication total
multiply *= number2;
// display number2 and final multiplication total
document.write(" * " + number2 + " = " + multiply);
答案 4 :(得分:0)
您可以这样做:
let number1, number2, result, resultText;
// prompt user to enter two numbers
number1 = prompt('Enter first number: ');
number2 = prompt('Enter a number bigger than first number: ');
// convert user input into numbers
number1 = Number(number1);
number2 = Number(number2);
// Initialize result and resultText
result = number1;
resultText = '' + number1;
// for loop to display numbers in between and multiply
for (let i = 1; i <= number2 - number1; i++) {
const temp = number1 + i;
result *= temp;
resultText += ' * ' + temp;
}
// display final multiplication total
resultText += ' = ' + result;
document.write(resultText);
答案 5 :(得分:0)
在这里找到for循环中给定两个数字之间所有数字的乘积, 简化代码
var num1 = 10;
var num2 = 20;
var product = 1;
document.write(num1);
num1 ++;
for (;num1 <= num2;) {
document.write(" * " + num1);
product *= num1;
num1++;
}
// display final product
document.write(" = " + product);