skip to Main Content

I’m trying to solve this problem, but my code does not work properly so here is the problem that I’m trying to solve.

Write a function addingAllTheWeirdStuff which adds the sum of all the
odd numbers in array2 to each element under 10 in array1. Similarly,
addingAllTheWeirdStuff should also add the sum of all the even
numbers in array2 to those elements over 10 in array1.

If any element in array2 is greater than 20, add 1 to every
element in array1.

And here is my code.

function addingAllTheWeirdStuff(array1, array2) {
  let arr1_2 = []
  let totalOds = 0
  let totalEvens = 0

  // suming th odds numbers in array2 
  totalOds = array2.reduce((acc, curr) => (curr % 2 !== 0 ? acc += curr : null, acc), 0)
  
  // suming th even numbers in array2
  totalEvens = array2.reduce((acc, curr) => (curr % 2 === 0 ? acc += curr : null, acc), 0)

  // adding the total to odd number in array1
  for (let i = 0; i < array1.length; i++) {
    let arods = array1[i]
    if (arods % 2 !== 0 && arods < 10) {
      arods += totalOds
      arr1_2.push(arods)
    }
  }

  // adding the total to even numbers in array1
  for (let j = 0; j < array1.length; j++) {
    let areven = array1[j]
    if (areven % 2 === 0 && areven > 10) {
      areven += totalEvens
      arr1_2.push(areven)
    }
  }
  return arr1_2
}

console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15], [1, 2, 3, 4, 5]));
// expected log [10, 12, 14, 23, 21]
console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15, 1], [1, 2, 3, 4, 5, 22]));
// expected log [11, 13, 15, 46, 44, 11]

2

Answers


  1. Instead of adding the sums of odd and even numbers your code adds it individually, I’ve made necessary changes to the code and it shall work

    function addingAllTheWeirdStuff(array1, array2) {
      let totalOdds = 0;
      let totalEvens = 0;
    
      // Sum the odd and even numbers in array2
      for (let i = 0; i < array2.length; i++) {
        if (array2[i] % 2 !== 0) {
          totalOdds += array2[i];
        } else if (array2[i] % 2 === 0) {
          totalEvens += array2[i];
        }
        
        if (array2[i] > 20) {
          for (let j = 0; j < array1.length; j++) {
            array1[j] += 1;
          }
        }
      }
    
      // Add the sums to elements in array1 as per the rules
      const result = [];
      for (let i = 0; i < array1.length; i++) {
        if (array1[i] < 10) {
          result.push(array1[i] + totalOdds);
        } else if (array1[i] > 10) {
          result.push(array1[i] + totalEvens)
        } else {
          result.push(array1[i])
        }
      }
    
      return result
    }
    
    console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15], [1, 2, 3, 4, 5]));
    // Expected output: [10, 12, 14, 23, 21]
    console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15, 1], [1, 2, 3, 4, 5, 22]));
    // Expected output: [11, 13, 15, 23, 21, 11]
    
    Login or Signup to reply.
  2. By having a little hole for values equal 10, according to the specs:

    … adds the sum of all the odd numbers in array2 to each element under 10 in array1. Similarly, addingAllTheWeirdStuff should also add the sum of all the even numbers in array2 to those elements over 10 in array1. [bold by NS]

    Take a first iteration for getting a bonus for a greater value and sum even and odd values.

    Later return a new array with even/odd or no offsets.

    function addingAllTheWeirdStuff(array1, array2) {
        let offset = 0,
            even = 0,
            odd = 0;
            
        for (const value of array2) {
            if (value > 20) offset++;
            if (value % 2) odd += value; else even += value;
        }
        even += offset;
        odd += offset;
        return array1.map(v => v + (v < 10 ? odd : v > 10 ? even : offset));
    }
    
    console.log(...addingAllTheWeirdStuff([1, 3, 5, 17, 15], [1, 2, 3, 4, 5])); // [10, 12, 14, 23, 21]
    console.log(...addingAllTheWeirdStuff([1, 3, 5, 17, 15, 1], [1, 2, 3, 4, 5, 22])); // [11, 13, 15, 46, 44, 11]
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search