Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Having trouble in JavaScript with Loops and Multiple Conditions

Here is the question:

"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."

I got the first part to work. I can't seem to figure out how to get the second part to work. Here is what I have tried:

function addingAllTheWeirdStuff(array1, array2){
  // ADD CODE HERE
  let sumOdd = 0;
  let sumEven = 0;
  for (let i = 0; i < array2.length; i++) {
    if (array2[i] % 2 !== 0) {
      sumOdd += array2[i];
    } else {
      sumEven += array2[i];
    }
  }
  for (let i =0; i < array1.length; i++) {
    if (array1[i] < 10) {
      array1[i] += sumOdd;
    } else if (array1[i] > 10) {
      array1[i] += sumEven;
    }
  for (let i =0; i < array2.length; i++) {
    if (array2[i] > 20) {
      for (let i =0; i < array1.length; i++) {
        array1[i] += 1;
        return array1;
      }
        return array1;
    }
  }
  return array1;
}

Any suggestions on how to make the last part of the question work?

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

First Of all I removed, some of the unnecessary braces and balanced them to make it more readable (at least to me)

Then I removed your last nested loop as it was unnecessary and inefficient. While you are looping through array2 to get sumOdd & sumEven you can check at the same time if any element is >20 and store that in a variable (greaterThan20). Then when adding the sums you can check that same var to add 1

function addingAllTheWeirdStuff(array1, array2) {


 // ADD CODE HERE
  let sumOdd = 0;
  let sumEven = 0;
  let greaterThan20 = false; // false by default 
  for (let i = 0; i < array2.length; i++) {
    if (array2[i] % 2 !== 0)
      sumOdd += array2[i];
    else
      sumEven += array2[i];
    if (array2[i] > 20)// check if any element > 20
      greaterThan20 = true;
  }
  for (let i = 0; i < array1.length; i++) {
    if (array1[i] < 10)
      array1[i] += sumOdd;
    else if (array1[i] > 10)
      array1[i] += sumEven;
    if (greaterThan20)// add the +1 if greater than 20
      array1[i] += 1;
  }
  return array1;
}
about 4 years ago · Juan Pablo Isaza Report

0

In your code as someone pointed out, you had a return statement in your inner loop which was breaking out of the loop.

I also added here an alternative solution, just for diversity. First calculate odd and even sums using filter and reduce. I also check if any element is over 20. Based on true/false value of arr2.some, I map over the arr1

const arr1 = [4, 5, 6, 7, 8, 9, 10, 11, 12]
const arr2 = [6, 7, 8, 9, 10, 11, 12, 13, 14, 21]

function addingAllTheWeirdStuff(arr1, arr2) {
  let oddSum = arr2.filter(el => el % 2 != 0).reduce((a, n) => a + n)
  let evenSum = arr2.filter(el => el % 2 == 0).reduce((a, n) => a + n)
  const over20 = arr2.some(el => el > 20)
  if (over20) {
    return arr1.map(el => el > 10 ? el += ++evenSum : el += ++oddSum)
  } else {
    return arr1.map(el => el > 10 ? el += evenSum : el += oddSum)
  }
}
console.log(addingAllTheWeirdStuff(arr1, arr2))

about 4 years ago · Juan Pablo Isaza Report

0

you were using return in the wrong place which leads the function to stop after the first number>20 instead of continuing for all numbers

you can also merge the first and the last loop, i just left it like this to be more clearer

function addingAllTheWeirdStuff(array1, array2) {
    // ADD CODE HERE
    let sumOdd = 0;
    let sumEven = 0;
    for (let i = 0; i < array2.length; i++) {
        if (array2[i] % 2 !== 0) {
            sumOdd += array2[i];
        } else {
            sumEven += array2[i];
        }
    }

    for (let i = 0; i < array1.length; i++) {
        if (array1[i] < 10) {
            array1[i] += sumOdd;
        } else if (array1[i] > 10) {
            array1[i] += sumEven;
        }

    }
    // moved this outside of the above loop
    for (let i = 0; i < array2.length; i++) {
        if (array2[i] > 20) {
            for (let i = 0; i < array1.length; i++) {
                array1[i] += 1;
             //   return array1;    remove this line
            }
            
        }
    }
   return array1;  // now return 
    
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!