“Schleife mit mehreren Bedingungen JS Codesmith” Code-Antworten

Schleife mit mehreren Bedingungen 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

Schleife mit mehreren Bedingungen 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

Ähnliche Antworten wie “Schleife mit mehreren Bedingungen JS Codesmith”

Fragen ähnlich wie “Schleife mit mehreren Bedingungen JS Codesmith”

Weitere verwandte Antworten zu “Schleife mit mehreren Bedingungen JS Codesmith” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen