所以我需要将这个循环中的每个数字相乘而我已经创建但它不应该以它应该的方式出现
//declare all variables
var enteredNumber;
var biggerEnteredNumber;
var totalValue;
var totalEnteredNumbers;
//Assign variables
totalEnteredNumbers = 0;
//Ask user for two numbers and the second to be bigger than the first
enteredNumber = prompt("Enter a number");
enteredNumber = Number(enteredNumber);
biggerEnteredNumber = prompt("Enter a number bigger than the first number");
biggerEnteredNumber = Number(biggerEnteredNumber);
for (enteredNumber = 1; enteredNumber < biggerEnteredNumber; enteredNumber++) {
document.write(enteredNumber + " * ");
totalEnteredNumbers = enteredNumber;
totalValue = totalEnteredNumbers * biggerEnteredNumber;
}
document.write(biggerEnteredNumber + " = " + totalValue);
}
如果用户输入3和7,我将如何乘以3 * 4 * 5 * 6 * 7 ??
答案 0 :(得分:0)
不要确定您需要多少,但这里有一些您可以使用的建议。我将text / appendChild设置为不在此处阻止的用户document.write
//declare all variables
var enteredNumber;
var biggerEnteredNumber;
var totalValue;
var totalEnteredNumbers;
var loopCounter;
var text;
//Assign variables
totalValue = 0;
text = "";
//Ask user for two numbers and the second to be bigger than the first
enteredNumber = prompt("Enter a number");
enteredNumber = Number(enteredNumber);
biggerEnteredNumber = prompt("Enter a number bigger than the first number");
biggerEnteredNumber = Number(biggerEnteredNumber);
for (loopCounter = 1; loopCounter < biggerEnteredNumber; loopCounter++) {
text += "<br/>"+loopCounter + " * ";
totalValue += (loopCounter*enteredNumber);
}
text += "<br/>"+biggerEnteredNumber + " = " + totalValue;
var div = document.createElement("div");
div.innerHTML=text;
document.body.appendChild(div);
&#13;
答案 1 :(得分:0)
我认为你正在尝试做这样的事情:
function multiplyvalues(from, to) {
var result = 0;
for(var i = from; i < to; i++) { result += (i * to);}
console.log(result);
}
最终在控制台中实现此功能:
乘数值(0,4);
答案 2 :(得分:0)
看看这段代码(不检查用户是否真的输入了数字):
//declare all variables
var v1;
var v2;
var total = 1; // start with 1 so mult. doesn't go wrong
var text = "";
//Ask user for two numbers and the second to be bigger than the first
v1 = prompt("Enter a number");
v1 = Number(v1);
do{ // while the second number is not bigger
v2 = prompt("Enter a number bigger than the first number");
v2 = Number(v2);
} while(v1 >= v2);
for (i = v1; i <= v2; i++) {
if(i < v2){
text += i + " * ";
}else{
text += i;
}
total = total * i;
}
alert(text + " = " + total); // 3 * 4 * 5 * 6 * 7 = 2520
我改为警报,但这并不重要。 Run jsfiddle