function sumOfNumbers(arrayOfNumbers) {
let arrayOfNumbers = [1, 2, 3, 4, 5, 6, 7]
const arrayLength = arrayOfNumbers.length;
let sum = 0;
for (i = 0; i <= arrayLength; i ) {
sum = sum arrayLength[i];
}
/*console.log(sumOfNumbers(sum))*/
return (sumOfNumbers(sum));
}
I used console.log nothing came up, I will appreciate if I am corrected.
CodePudding user response:
It's not. First:
function sumOfNumbers(arrayOfNumbers) {
let arrayOfNumbers = [1, 2, 3, 4, 5, 6, 7]
you're calling the function with an argument arrayOfNumbers
, yet overwrite its value in the first line. Last,
return (sumOfNumbers(sum));
you're calling this function from within the function. It's recursion. But, since there's no path where the function simply returns a value, without calling itself again, it's infinite recursion, which leads to a call stack exceeded
error.
CodePudding user response:
You have many mistakes in your code. Added comments in the code below to say what you did wrong and what you need to actually do.
function sumOfNumbers(arrayOfNumbers) {
const arrayLength = arrayOfNumbers.length;
let sum = 0;
// you need to declare i so it is not a global
// Indexes start at ZERO so the <= arrayLength would cause you to reference one past the length of the array
for (let i = 0; i < arrayLength; i ) {
// you are trying to read arrayOfNumbers... wrong, needs to be the array
sum = sum arrayOfNumbers[i];
}
// return the sum
return sum;
}
// define your array of numbers outside
const myNumbers = [1, 2, 3, 4, 5, 6, 7];
// call the method passing in the array
console.log(sumOfNumbers(myNumbers));
CodePudding user response:
Definitely not going to work. Let me explain.
You have:
function sumOfNumbers(arrayOfNumbers) {
let arrayOfNumbers = [1, 2, 3, 4, 5, 6, 7]
const arrayLength = arrayOfNumbers.length;
let sum = 0;
for (i = 0; i <= arrayLength; i ) {
sum = sum arrayLength[i];
}
/*console.log(sumOfNumbers(sum))*/
return (sumOfNumbers(sum));
}
Let's break the issues down.
The first thing I can see is that you are overwriting arrayOfNumbers
at the beginning of your function. Don't do that. If you are trying to set a default value, you can do that by updating your function declaration to function sumOfNumbers(arrayOfNumbers = [1, 2, 3, 4, 5, 6, 7]) {...}
.
The other issue is within the for
loop. You are trying to get the i
th index of arrayLength
which is just a number and not an array. So you have to switch that to arrayOfNumbers
.
Lastly, you are returning sumOfNumbers(sum)
. You should just return the sum
variable and that should be it.
Here is a working example with the code fixed:
function sumOfNumbers(arrayOfNumbers) {
const arrayLength = arrayOfNumbers.length;
let sum = 0;
for (i = 0; i < arrayLength; i ) {
sum = sum arrayOfNumbers[i];
}
return sum;
}
let a = sumOfNumbers([1, 2, 3, 4, 5, 6, 7]);
console.log(a);
CodePudding user response:
Try this:
function myFunction() {
const arrayOfNumbers = [1, 2, 3, 4, 5, 6, 7];
const arrayLength = arrayOfNumbers.length;
let sum = 0;
for (i = 0; i < arrayLength; i ) {
sum = sum arrayOfNumbers[i];
}
return sum;
}
JSFiddle: https://jsfiddle.net/3g6psLwj/