0

I am a beginner in Javascript. I did some research and still cannot figure out how to return the sum of array1 and add it to each number in array2. My code adds the sum of both arrays instead. I want the expected log to be [11, 13, 15]

function addingAllTheWeirdStuff(array1, array2){
        let list = []
        let sum = 0
        for (let i = 0; i < Math.max(array1.length, array2.length); i++){
            list.push(sum += array1[i])
            sum += array2[i]
        } return sum
    }
    
    console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

5 Answers 5

0
  • You can get sum of array using Array.reduce function.
  • And can add sum to each item of array2 using Array.map function.

function addingAllTheWeirdStuff(array1, array2){
  const sum = array1.reduce((sum, cur) => sum + cur, 0);
  return array2.map(item => item + sum);
}

console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6]));

Sign up to request clarification or add additional context in comments.

4 Comments

You are missing the 2nd argument for reduce()
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… - If no initialValue is supplied, the first element in the array will be used as the initial accumulator value and skipped as currentValue
You should use an initial value 0 in case the array is empty.
Thanks. @Barmar missed about that.
0

You need two separate loops. One loop calculates the sum of array1. The second loop adds that to every element of array2.

You can't combine the loops because you need the entire sum of the first array to add to the first element of the second array. Your code just adds the current value of the running total, not the final total. It's also adding elements of both arrays to sum, not adding sum to the elements of the second array.

Also, you're returning the sum, not the new array.

function addingAllTheWeirdStuff(array1, array2) {
  let sum = 0;
  for (let i = 0; i < array1.length; i++) {
    sum += array1[i];
  }
  return array2.map(el => el + sum);
}

console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

Comments

0

There is a logical issue in your code. The reason why your code behaves wrongly is that you are trying to do, in the same for cycle, two activities that should be performed in different cycles. I'll try to explain it directly with the code:

// WHAT YOU SHOULD DO:

function addingAllTheWeirdStuff(array1, array2){
  let sum = 0;
  //First compute the sum of the first array
  for (let i = 0; i < array1.length; i++){
     sum += array1[i]; //this is equivalent to sum=sum+array[i]
  }
  //Then add the sum to each element of array 2:
  for (let i = 0; i < array1.length; i++){
     array2[i]+=sum; 
  }
  //Finally, return the updated array
  return array2;      
}
    
    console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

//WHAT YOU HAVE DONE:

function addingAllTheWeirdStuff(array1, array2){
        let list = []
        let sum = 0
        //if array1.lenght!==array2.lenght this cycle will cause problems
        for (let i = 0; i < Math.max(array1.length, array2.length); i++){
            //at each cycle of the for you are saving the value of sum
            //in list. It's not useful to compute sum.
            list.push(sum += array1[i])
            //Here you are doing: sum = sum+array2[i];
            //and not array2[i]=array2[i]+sum;
            //furthermore this should be done in a different for cycle
            sum += array2[i]
        } return sum
    }
    
    console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

Comments

0

function addingAllTheWeirdStuff(array1, array2) {
  if(array1 == null || array2 == null) return;

  const sumArray1 = array1.reduce((list, value) => list + value);
  return array2.map(value => value + sumArray1);
}

console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

Comments

0

I think this will help you.

function addingAllTheWeirdStuff(array1, array2) {
        //firt get total of first array with reduce function
        const total = array1.reduce((accumulator, item) => {
            return accumulator + item;
        }, 0)
        const list = []
        //then add total to second function with map function.
        array2.map((e) => list.push(e + total))
        return list
    }
    
    console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.