From f450a66d60ec0c67c1627dcf0153009dde8041bf Mon Sep 17 00:00:00 2001 From: Aizhan87 Date: Mon, 2 Sep 2019 09:58:04 -0700 Subject: [PATCH] solution for evenOddNums --- session3/index_extra.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/session3/index_extra.js b/session3/index_extra.js index 3d874498..589ddb06 100644 --- a/session3/index_extra.js +++ b/session3/index_extra.js @@ -7,19 +7,19 @@ // Return a sum of all parameters entered regardless of the amount of numbers - NO ARRAYS // ex. addAll(2,5,6,7) === 20 -function addAll() {} +function addAll() { } // CHALLENGE 2: SUM ALL PRIMES // Pass in a number to loop up to and add all of the prime numbers. A prime number is a whole number greater than 1 whose only factors are 1 and itself // ex. sumAllPrimes(10) == 17 -function sumAllPrimes() {} +function sumAllPrimes() { } // CHALLENGE 3: SEEK & DESTROY // Remove from the array whatever is in the following arguments. Return the leftover numbers in an array // ex. seekAndDestroy([2, 3, 4, 6, 6, 'hello'], 2, 6) == [3, 4, 'hello'] -function seekAndDestroy() {} +function seekAndDestroy() { } // CHALLENGE 4: SORT BY HEIGHT // Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. @@ -28,11 +28,11 @@ function seekAndDestroy() {} // sortByHeight(a) == [-1, 150, 160, 170, -1, -1, 180, 190] function sortByHeight() { -// SOLUTION BY Smollet777 -// filtering out and sorting all "humans" -// const humans = a.filter(v => v !== -1).sort((a, b) => a - b) -// put sorted "humans" back -// return a.map(v => v !== -1? humans.shift() : -1) + // SOLUTION BY Smollet777 + // filtering out and sorting all "humans" + // const humans = a.filter(v => v !== -1).sort((a, b) => a - b) + // put sorted "humans" back + // return a.map(v => v !== -1? humans.shift() : -1) } // CHALLENGE 5: MISSING LETTERS @@ -79,4 +79,16 @@ console.log('Missing: ' + missingLetters(letters)) // `Missing: l, q`; // ex. // evenOddSums([50, 60, 60, 45, 71]) == [170, 116] -function evenOddSums() {} +function evenOddSums(arr) { + // SOLUTION BY Aizhan87 + let evenNums = 0; + let oddNums = 0; + for (let i = 0; i < arr.length; i++) { + if (arr[i] % 2 === 0) { + evenNums += arr[i] + } else { + oddNums += arr[i] + } + } + return [evenNums, oddNums] +}