“boucle avec plusieurs conditions js codesmith” Réponses codées

boucle avec plusieurs conditions js codesmith

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))
 Run code snippet
Harry Fahringer III

boucle avec plusieurs conditions js codesmith

// 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] );
Harry Fahringer III

Réponses similaires à “boucle avec plusieurs conditions js codesmith”

Questions similaires à “boucle avec plusieurs conditions js codesmith”

Plus de réponses similaires à “boucle avec plusieurs conditions js codesmith” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code