From 2160b76508babb6e89800285cb02801af995fbca Mon Sep 17 00:00:00 2001 From: Ibrahim Mattar Date: Fri, 5 Feb 2021 01:17:20 +0200 Subject: [PATCH 1/2] Delete sumOfPairNumber.js --- sumOfPairNumber.js | 54 ---------------------------------------------- 1 file changed, 54 deletions(-) delete mode 100644 sumOfPairNumber.js diff --git a/sumOfPairNumber.js b/sumOfPairNumber.js deleted file mode 100644 index 87d3c0d..0000000 --- a/sumOfPairNumber.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * write a function that returns the sum of the pair numbers inside an array - * - * exple sumPair([1,6,100,346,761,249])=>452 - * - * sumPair([2,4,9,73])=>6 - */ -function sumPair(array) { - - let total = 0 - - if(Array.isArray(array)){ // for array - for(i of array){ - if(i % 2 == 0){ - total = total + i - } - } - return total - }else if(typeof array == "object"){ // can replace it by if(true) it works too for object - for(let key in array){ - if(array[key] % 2 == 0){ - total = total + array[key] - }; - } - return total - } -} - - - console.log(sumPair([1,6,100,346,761,249])); - console.log(sumPair({ - a:2, - b:5, - c:8, - d:10 - })); - /* - * bonus points - * - * same function works for both arrays and objects - * - * if the input is an array return the sum of the pair numbers - * - * if it is an object like this: - * - * obj={ - * a:2, - * b:5, - * c:8 - * } - * - * return => "a & c have pair values" - * - */ \ No newline at end of file From 76f7e034acb3b3bd2d619d4c36712d2d22a06450 Mon Sep 17 00:00:00 2001 From: Ibrahim Mattar Date: Fri, 5 Feb 2021 01:25:12 +0200 Subject: [PATCH 2/2] Update sumOfPairNumber.js --- sumOfPairNumber.js | 53 ++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/sumOfPairNumber.js b/sumOfPairNumber.js index 87d3c0d..b452894 100644 --- a/sumOfPairNumber.js +++ b/sumOfPairNumber.js @@ -1,39 +1,28 @@ -/* - * write a function that returns the sum of the pair numbers inside an array - * - * exple sumPair([1,6,100,346,761,249])=>452 - * - * sumPair([2,4,9,73])=>6 - */ -function sumPair(array) { - let total = 0 +let Arr1 = [1, 24, 3, 4, 5, 6,"C", 73, 8, 19]; +let Arr2 = {a:1, b:24, c:3, d:4, e:5, f:6, g:73, h:8, i:19}; - if(Array.isArray(array)){ // for array - for(i of array){ - if(i % 2 == 0){ - total = total + i - } +function sumEven(array) { + let sum = 0 + if(Array.isArray(array)){ + for(i of array){ + if(i % 2 === 0){ + sum = sum + i + } } - return total - }else if(typeof array == "object"){ // can replace it by if(true) it works too for object - for(let key in array){ - if(array[key] % 2 == 0){ - total = total + array[key] - }; - } - return total - } + return sum; + } + else if (typeof array ==="object") { + for (let key in array){ + if (array[key] % 2 === 0){ + (sum = sum + array[key]) + } + } + return sum; + } } - - - console.log(sumPair([1,6,100,346,761,249])); - console.log(sumPair({ - a:2, - b:5, - c:8, - d:10 - })); + console.log(sumEven(Arr1)); + console.log(sumEven(Arr2)); /* * bonus points *