Summing two arrays for an exercise

This exercise in my bootcamp is a little tricky. I believe I have the basics but I’d like some feedback. I don’t think I’m using the right for loop. Or I have the array variables setup wrong. I think I’m also adding them wrong.

function sumArray ([arr1], [arr2]) {
    for (let arr1 = 0; arr1 > 5; arr1++)
     console.log([arr1] + [arr2]);

Can you translate this code into words that describe what you think this line is doing? I’m not sure how you intend this line to function.

Edit: Here is the actual exercise. I don’t know why didn’t add this before. Sorry.

Write a function called sumArray which accepts a single an array of numbers as an argument. It should return the sum of all the numbers in the array.

As for the loop, I don’t know. lol. A for loop isn’t needed because there isn’t any looping happening. I see that now.

Well, to start, lets get this part right. I see two arguments for this function. Also, the arguments shouldn’t have brackets around them.

I appreciate you walking me through this. I think it needs two arguments/parameters because it needs to add two. As for the brackets, how else do I tell the program they’re array elements?

This says that there should be a single argument that is an array of numbers though?

Wow, lol. Okay I need to take control of my ADHD and reading carefully otherwise I have 0% chance to make a penny in this career path. My fault and I need to re-read directions.

As for the brackets, you don’t use brackets when declaring a variable that holds an array. Function arguments are like this.

We use this:

const myArray = [1, 2, 3];

Not this:

const [myArray] = [1, 2, 3];


function myFunction(myFirstArg, mySecondArg, myThirdArg) {
  // Function body goes here

i think you lack some basics and it will be good idea to check on the FCC JS curriculum. There are many loopholes in your code and your logic.
Regarding the challenge at hand, lets say we have the array arr=[2,5,3,7]. You want to sum all its elements. How would you target the element at the second position? (5: arr[1]). How would you combine the first and second elements? arr[0]+arr[1]. There is also the basic task of creating a loop which iterates throught the elements of an array. The array length would determine how many iterations the loop will have(for array with 5 elements, we loop 5 times, for 7 elements, we loop 7 etc).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.