Javascript Challenge: Loops - Multiple Conditions - stuck and can't figure this out

Viewed 605

I did this module on functions and execution context - all questions have gone well but there is one challenge I have spent a lot of time on and still can't figure it out. Any help will be greatly appreciated. Thank you

Challenge question says: 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.

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

// Uncomment these to check your work!
// 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]

// my attempt so far:
function addingAllTheWeirdStuff(array1, array2) {
  // ADD CODE HERE
  let result = []
  for (let i = 0; i < array2.length; i++) {
    if (array2[i] > 20) {
      result = array1[i] += 1
    }
  }
  for (let i = 0; i < array2.length; i++) {
    if (array2[i] % 2 === 0 && array1[i] > 10) {
      result = array1[i] + array2[i]
    }
  }
  for (let i = 0; i < array2.length; i++) {
    if (array2[i] % 2 !== 0 && array1[i] < 10) {
      result = array1[i] + array2[i]
    }
  }
  return result
}

2 Answers

You can easily achieve this using reduce and map array method, with the ternary operator:

const array1 = [1, 3, 5, 17, 15];
const array2 = [1, 2, 3, 4, 5];

function addingAllTheWeirdStuff(array1, array2) {

    const oddSum = array2.reduce((sum, current) => current % 2 ? current + sum : 0 + sum, 0)
    const oddEven = array2.reduce((sum, current) => current % 2 == 0 ? current + sum : 0 + sum, 0)

    return array1.map(num => num < 10 ? num + oddSum : num + oddEven)

}

console.log(addingAllTheWeirdStuff(array1, array2))

If you break the challenge into smaller pieces, you can deconstruct it better and come up with your solutions.

This is what I did... I will be adding more comments shortly with more explanations

I chose to keep using loops as I assumed this was the point of the challenges (to practice for loops, multiple conditions, etc) - In other words, I chose to not use map / reduce on purpose but if that's allowed, use the answer by @charmful0x as it results in less code :)

// function to get sum of all odd numbers in array
function getSumOfAllOddNumbersInArray( elementArray ){
  var sumOfOddNumbers = 0;
  
  for (let i = 0; i < elementArray.length; i++) {
    // use remainder operator to find out if element is odd or not
    if (elementArray[i] % 2 !== 0 ) {
      sumOfOddNumbers += elementArray[i];
    }
  }
  
  return sumOfOddNumbers;
}

// function to get sum of all EVEN numbers in array
function getSumOfAllEvenNumbersInArray( elementArray ){
  var sumOfEvenNumbers = 0;
  
  for (let i = 0; i < elementArray.length; i++) {
    // use remainder operator to find out if element is odd or not
    if (elementArray[i] % 2 === 0 ) {
      sumOfEvenNumbers += elementArray[i];
    }
  }
  
  return sumOfEvenNumbers;
}

// Return true if there is at least one element in array that is greater than 20
function hasElementOverTwenty( elementArray ){
  for (let i = 0; i < elementArray.length; i++) {
    if (elementArray[i] > 20 ) {
      // no need to keep looping, we found one - exit function
      return true;
    }
  }
  
  return false;
}


function addingAllTheWeirdStuff( firstArray, secondArray ){
  var sumOfOddNumbersInArray = getSumOfAllOddNumbersInArray( secondArray );
  var sumOfEvenNumbersInArray = getSumOfAllEvenNumbersInArray( secondArray );
  var needToAddOne = hasElementOverTwenty( secondArray );
    
  for (let i = 0; i < firstArray.length; i++) {
    // Challenge One
    if (firstArray[i] < 10) {
      firstArray[i] = firstArray[i] + sumOfOddNumbersInArray;       
      
    } else if (firstArray[i] > 10) {
    // Challenge Two
      firstArray[i] = firstArray[i] + sumOfEvenNumbersInArray;       
    }
    
    // bonus
    if( needToAddOne ){
      firstArray[i]++;
    }   
  }
  
  return firstArray;
}

// Uncomment these to check your work!
console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15], [1, 2, 3, 4, 5])); 
console.log('expected:' + [10, 12, 14, 23, 21] );
console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15, 1], [1, 2, 3, 4, 5, 22]));
console.log('expected:' + [11, 13, 15, 46, 44, 11] );
Challenge question says: 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.

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

Related