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]

Before figuring out what code to use, describe to us the steps your function will need to take to accomplish the task. I think once you write down the steps, you will see only a single loop is needed to accomplish the task.

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.

Yes, I understand that. See if you can describe your manual process of the steps taken to accomplish the task. Explain how you will iterate through the array and decide which elements get added to each other. This logic will form the basis of the code you will write, but you must first completely understand what steps will actually accomplish the task.

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?

I think you misunderstood what I was asking you to do. When I give you a list of numbers (say 1, 2, 3, 4, 5, 6) and ask you to create a new list that contains sums of neighboring items, can you describe how you actual create the final list of 3, 7, 11? What ever process you are using to calculate the numbers of this list is the basis of how you will approach this with code. What I mean by describe your steps is literally how to you go about coming up with the first number (3) and then the second number (7) and the last number (11)? If you write out the steps, you should start to see a pattern. Without describing or talking about any code, try to describe the process you would do on pen and paper to create this list. Once you write those steps out, I can help guide you to figuring out the complete algorithm (and then you can code however you want).

Just give it a try. Just use plain language and no coding terms.

  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]```