Home > database >  Having trouble in JavaScript with Loops and Multiple Conditions
Having trouble in JavaScript with Loops and Multiple Conditions

Time:10-16

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.

CodePudding user response:

You're nearly there. Wile testing for odd or even, you can also test for >20. In the second loop, just add those numbers too.

function addingAllTheWeirdStuff(array1, array2){
  let sumOdd = 0;
  let sumEven = 0;

  // ==> EXTRA VARIABLE
  let great20 = 0;

  for (let i = 0; i < array2.length; i  ) {
    if (array2[i] % 2 !== 0) {
      sumOdd  = array2[i];
    } else {
      sumEven  = array2[i];
    }
    // ===> TEST if >20, If so, add one;
    if(array2[i] > 20)   great20;
   }
   
  for (let i =0; i < array1.length; i  ) {
    // ==> ADD great 20
    array1[i]  = great20;
    if (array1[i] < 10) {
      array1[i]  = sumOdd;
    } else if (array1[i] > 10) {
      array1[i]  = sumEven;
    }
        return array1;
    }
   return array1;
}

CodePudding user response:

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;
}

CodePudding user response:

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 
    
}

CodePudding user response:

const addingAllTheWeirdStuff=(a,b)=>a.map(i=>(i>10?i b.reduce((s,c)=>s ((c%2)?c:0),0):((i<10)?i b.reduce((s,c)=>s (!(c%2)?c:0),0):i)) b.some(e=>e>20));
  • Related