Adding Array for sum of neighbors

Hello Friends,

Once again I am stuck on setting up the for loop and inner for loop on this array. I know there is more advanced ways to do this but I am learning the basics here. Where am I going wrong ?

Question/Challenge
Write a function called addNeighbors that takes an array of numbers and adds each number in the array to the number next to it. The array will always have an even number of numbers in it. In the end you should return an array of neighbors added up.

function addNeighbors([num]){
 const arr = num;
 sum = 0;
 for (i=0; i < arr.length; i+2){
  for (j=1; j <arr.length; j+2){
    } 
  sum += arr[i] + arr[j];
  }
 return num;
}

addNeighbors([1,2,3,4,5,6]);




//addNeighbors([1, 2, 3, 4, 5, 6]) // should return [3, 7, 11]
//addNeighbors([12, 32, 35, 28, 10, 18, 55, 781]) // should return [44, 63, 28, 836]

I think you are missing on interpretatation

you are returning a number

plus a few syntax/logic errors


first thing, if the input is [1,2,3,4,5,6] can you write the output?

The output is [3, 7, 11]
And to get there I will need two loops that add to each other from a single array to output a new array. Do I need splice or slice here?

why do you need two loops? how many numbers do you need to sum?

Hard coding there will be 3 sums in the new array.
[sum1, sum2, sum3]
[1+2, 3+4,5+6]

If your not using two loops to add these integers, how can you do it with one loop? and avoid hard coding?

return output
[3,7,11]

Clearly that’s why I posted it here. The function will need to pass the array and add each of its neighbors together through an iteration and output the sum of each sets of neighbors. As described in the previous comments.

Sure, it’s like asking me to build an airplane. I have no idea. I usually learn by trial and error …you know breaking things and putting them back together. In this case code it to work then figure out where and why it works in that manner.

To satisfy your question without knowing all the JS universe.

-Create a function to pass an array function addNeighbors([num])

-Create a constant variable to declare and assign the array const arr1=num

-Create a a new array for the sum of neighbors to go after the loop iterates let array = [];

-Eliminate the nested loop

-Write the for loop to iterate through the array and add the neighbors and place these sums into the new array

  array = arr1[i] + arr1[i+1];

-See my new code below. 
```function addNeighbors([num]){
 const arr1 = num;
let array = [];
 for (i=0; i < arr1.length-1; i++){
  array = arr1[i] + arr1[i+1];
 }
 return array;
}

output = addNeighbors([1,2,3,4,5,6]);
console.log(output);

All I am getting is an empty array

what’s happening here? why that argument?

  1. if array is a string and later assign to array = arr1[i] + arr1[i+1] , this overwrite everytime the array with new value.
  2. the array need a function to manipulate with them.
    How to add an object to an array in JavaScript ? - GeeksforGeeks

Because the output called outside the function
output = addNeighbors([1,2,3,4,5,6]); so I took it as I had to use the same syntax when creating the function function addNeighbors([num]){

no, that’s not going to work. Doing like that you are taking only the first element of the array, you have accidentally used array destructuring

you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn

Ok, fudged it around and got the answer but. How do I get the comma delimeter to show up between sums.

function addNeighbors(num){
 const arr1 = num;
let array = [];
 sum = 0;
 for (i=0; i < arr1.length-1; i+=2){
  array += arr1[i] + arr1[i+1];
  
 }
 return array;

}
addNeighbors([1, 2, 3, 4, 5, 6]) // should return [3, 7, 11]

This is what is getting returned

=> ‘3711’

Push the values into the array instead of using +=.

Push.

Yessssss… Thank you it worked.

 const arr1 = num;
let array = [];
 sum = 0;
 for (i=0; i < arr1.length-1; i+=2){
  array.push(arr1[i] + arr1[i+1]);
  
 }
 return array;

}
addNeighbors([1, 2, 3, 4, 5, 6]) // should return [3, 7, 11]

//addNeighbors([12, 32, 35, 28, 10, 18, 55, 781]) // should return [44, 63, 28, 836]```