I'm getting a better understanding of functions, arrays and for loops. I'm looking to create a function which takes in an array of numbers and returns the total of all the items in the array added together. I figured out how to total an array.
我对函数、数组和循环有了更好的理解。我希望创建一个函数,它接收数组中的数字,并返回数组中所有项的总和。我算出了一个数组的总数。
var myArray = [1, 2, 3, 4, 5];
var myTotal = 0;
for (i = 0; i < myArray.length; i++) {
myTotal += myArray[i];
}
console.log (myTotal);
I know how to create/call a function, however when I attempt to add the two concepts together it never works. Are you able to place an array into the parameters of a function?
我知道如何创建/调用一个函数,但是当我尝试将两个概念加在一起时,它就不起作用了。您是否能够将数组放入函数的参数中?
More or less I want to create a code concept which takes on an array of numbers into a function which totals the array. Any help would be greatly appreciated and thanks for taking the time to read my long winded question.
我或多或少地想要创建一个代码概念,它将一个数字数组转换为一个函数,该函数包含数组。任何帮助都将非常感谢,感谢您花时间阅读我冗长的问题。
4 个解决方案
#1
1
The short answer is yes. Any value, including an array reference, can be passed into a function.
简而言之,答案是肯定的。任何值,包括数组引用,都可以传递到函数中。
Here's what's needed in your case:
以下是你的案例中需要的:
var myArray = [1, 2, 3, 4, 5];
function totalArray(arr) {
var myTotal = 0;
for (var i = 0; i < arr.length; i++) {
myTotal += arr[i];
}
return myTotal;
}
document.getElementById("value").innerText = totalArray(myArray);
<div id="value"></div>
#2
0
var myArray = [1, 2, 3, 4, 5];
function totalArray(arr) {
var myTotal = 0;
arr.forEach(number){myTotal +=number};
return myTotal;
}
console.log(totalArray(myArray));
Instead of a for loop you can also use for Each as shown above.
您也可以使用上面所示的for循环来代替for循环。
//btw I guess the mistake was just expecting the code to run instead of telling it to run.
//顺便说一下,我想错误只是期望代码运行而不是告诉它运行。
Javascript has its problems put every functionality in a function then execute it, is the normal way of doing things. While the snipet might run here, some things are needed in actual code for it to run
Javascript有它的问题把每个功能都放在一个函数中然后执行它,这是正常的做法。虽然snipet可能在这里运行,但实际运行的代码中需要一些东西。
#3
0
You can do it:
你可以这么做:
var myArray = [1, 2, 3, 4, 5];
function sum(arr) {
return arr.reduce(function(a,b){return a + b;});
}
console.log(sum(myArray));
#4
-1
See if this helps :
//function
function getTotal(myArray){
var myTotal = 0;
for (i = 0; i < myArray.length; i++) {
myTotal += myArray[i];
}
return myTotal;
}
//function call
var myArray = [1, 2, 3, 4, 5];
var total = getTotal(myArray);
console.log(total);
console.log(总);
#1
1
The short answer is yes. Any value, including an array reference, can be passed into a function.
简而言之,答案是肯定的。任何值,包括数组引用,都可以传递到函数中。
Here's what's needed in your case:
以下是你的案例中需要的:
var myArray = [1, 2, 3, 4, 5];
function totalArray(arr) {
var myTotal = 0;
for (var i = 0; i < arr.length; i++) {
myTotal += arr[i];
}
return myTotal;
}
document.getElementById("value").innerText = totalArray(myArray);
<div id="value"></div>
#2
0
var myArray = [1, 2, 3, 4, 5];
function totalArray(arr) {
var myTotal = 0;
arr.forEach(number){myTotal +=number};
return myTotal;
}
console.log(totalArray(myArray));
Instead of a for loop you can also use for Each as shown above.
您也可以使用上面所示的for循环来代替for循环。
//btw I guess the mistake was just expecting the code to run instead of telling it to run.
//顺便说一下,我想错误只是期望代码运行而不是告诉它运行。
Javascript has its problems put every functionality in a function then execute it, is the normal way of doing things. While the snipet might run here, some things are needed in actual code for it to run
Javascript有它的问题把每个功能都放在一个函数中然后执行它,这是正常的做法。虽然snipet可能在这里运行,但实际运行的代码中需要一些东西。
#3
0
You can do it:
你可以这么做:
var myArray = [1, 2, 3, 4, 5];
function sum(arr) {
return arr.reduce(function(a,b){return a + b;});
}
console.log(sum(myArray));
#4
-1
See if this helps :
//function
function getTotal(myArray){
var myTotal = 0;
for (i = 0; i < myArray.length; i++) {
myTotal += myArray[i];
}
return myTotal;
}
//function call
var myArray = [1, 2, 3, 4, 5];
var total = getTotal(myArray);
console.log(total);
console.log(总);