我是JavaScript的新手,并且正在愚弄真正了解基础知识。现在我尝试制作一个计算器。一个非常基本的,可以添加,subrtact,devide和multiply。我已经使用它来使用此代码(仅显示乘法:
var multiply = function () {
var numbers = prompt("How many numbers do you want to multiply?","At least 2, max 4");
numbers = Number(numbers);
switch (numbers){
case 2:
num1 = prompt("Your first number: ");
num2 = prompt("Your second number: ");
ans = Number(num1) * Number(num2);
alert(num1 + " * " + num2 + " = " + ans);
break;
case 3:
num1 = Number(prompt("Your first number: "));
num2 = Number(prompt("Your second number: "));
num3 = Number(prompt("Your third number: "));
ans = Number(num1) * Number(num2) * Number(num3);
alert(num1 + " * " + num2 + " * " + num3 + " = " + ans);
break;
case 4:
num1 = Number(prompt("Your first number: "));
num2 = Number(prompt("Your second number: "));
num3 = Number(prompt("Your third number: "));
num4 = Number(prompt("Your fourth number: "));
ans = Number(num1) * Number(num2) * Number(num3) * Number(num4);
alert(num1 + " * " + num2 + " * " + num3 + " * " + num4 + " = " + ans);
break;
default:
alert("Not valid");
break;
}
};
multiply();

我的问题是,当涉及用户可以繁殖的数量时,我非常有限。为每个可能的数量制作一个开关案例需要一段时间,所以我想到了这个:
var multiply = function () {
var numbers = [];
var ans = 0;
var times = prompt("How many numbers do you want to multiply?");
for(var i = 0; i<times; i++){
Number(numbers.push(prompt("Please, enter one of your numbers")));
}
alert(ans);
};
multiply();
&#13;
所以,我的问题是:我怎样才能得到&#34; ans&#34;等于我的数组中的每个元素&#34;数字&#34;与海誓山盟成倍增加?
答案 0 :(得分:2)
您可以使用reduce功能:
[1, 2, 3, 4].reduce(function(a, b) {
return a * b;
}); // it return 24
顺便说一下。在你的循环中你应该以这种方式推送到数组:
for(var i = 0; i<times; i++){
numbers.push(Number(prompt("Please, enter one of your numbers")));
}
答案 1 :(得分:1)
如其他答案中所述,您可以使用Array.reduce
method。但是,您可以使用原生Math.imul
method:
var numbers = [1, 2, 3, 4];
var ans = numbers.reduce(Math.imul);
console.log(ans);
答案 2 :(得分:0)
如果我理解你,你想要多样化的东西([1,2,3,4])=== 24? 然后,您可以使用https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
答案 3 :(得分:0)
您可以继续询问一个数字并同时显示中间结果。用户可以使用escape退出:
var multiply = function () {
var s, ans = 1;
while (s = prompt('Current product is ' + ans +
'. Enter next factor to multiply with, or hit escape to exit')) {
ans *= Number(s);
}
}
multiply();
答案 4 :(得分:0)
减少可能是正确的答案,但为了让您更全面地了解它实际上在做什么,请看一下。这就是我手动做基本相同的事情,添加一些防护装置以使其更安全。
//This is an enum. It's basically a cleaner and more
//scalable way to define constants. Here I'm using an
//integer to represent each of the four operations
var OPERATIONS = {
'ADD': 1,
'SUBTRACT': 2,
'MULTIPLY': 3,
'DIVIDE': 4
};
function calc (operation, values)
{
if (!operation || !values || values.length < 2)
{
//The inputs aren't valid, so throw some kind of error
}
//This will be used in all of our cases, so
//we define it at a larger scope
var result;
switch (operation)
{
case OPERATIONS.MULTIPLY:
//Extracts the first value and stores it
result = values.shift ();
//Iterate through the remaining values.
//Remember that the first value is no
//longer in the set
values.forEach (function (value, index)
{
//*= is a unary operator which multiplies a value by
//the operand, and then stores it back in itself.
//This is equivalent to result = result * value.
result *= value;
});
break;
//Create cases for ADD, SUBTRACT, and DIVIDE
}
return result;
}
//Test cases
console.log (calc (OPERATIONS.ADD, [1, 1]); //Prints 2
console.log (calc (OPERATIONS.SUBTRACT, [10, 1, 1]); //Prints 8
console.log (calc (OPERATIONS.MULTIPLY, [1, 2, 3, 4]); //Prints 24
console.log (calc (OPERATIONS.ADD, [calc (OPERATIONS.MULTIPLY, [5, 5], 3, 100]); //Prints 128
如果你想做这样的事情,你可以让它更通用一点......
function calc2 (operations, values)
{
//You need one more value than operation here
if (!operations || !values || values.length < 2 || (values.length - operations.length !== 1))
{
//The inputs aren't valid, so throw some kind of error
}
var result = values.shift ();
while (values.length)
{
switch (operations[0])
{
case OPERATIONS.ADD:
result += values[0]
break;
case OPERATIONS.SUBTRACT:
result -= values[0]
break;
case OPERATIONS.MULTIPLY:
result *= values[0]
break;
case OPERATIONS.DIVIDE:
result /= values[0]
break;
default:
//Something has gone horribly wrong. Thrown an error
}
//Work your way down the array by continually
//removing the first value
values.shift ();
operations.shift ();
}
//Note that this method solves the equation linerally;
//BEDMAS (or PEMDAS, depending on where you're from)
//is not honoured.
return result;
}
//Test cases
console.log (calc ([OPERATIONS.ADD], [1, 1])); //Prints 2
console.log (calc ([OPERATIONS.ADD, OPERATIONS.ADD], [1, 2, 3])); //Prints 6
console.log (calc ([OPERATIONS.ADD, OPERATIONS.ADD, OPERATIONS.DIVIDE], [6, 7, 5, 3])); //Prints 6
通过逐个存储输入和操作来使用第二个函数。所以你得到类似6 + 7 + 5 / 3 =
的东西,然后将它分解成各自的组件来进行计算。
这里的一般方法是你想获得一个基础值,然后在它上面迭代以得到你的最终结果。在第一种情况下,这意味着对每个值使用相同的操作来改变值。在第二种情况下,你告诉它你想要在每一步而不是在开始时执行的突变类型。
如果要将其概括为使用过的BEDMAS或具有更复杂的功能,则可能必须创建一个树结构,其中每个节点表示一个操作及其各自的操作数,并且为了获得结果,您只需遍历树。
e.g。 PLUS(PLUS(DIVIDE(5, 3), 7), 6)