forked from MenaSchool/ToyProblems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumOfPairNumber.js
More file actions
70 lines (65 loc) · 1.36 KB
/
sumOfPairNumber.js
File metadata and controls
70 lines (65 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* 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) {
//your code goes here
let sum = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] % 2 === 0) {
sum += array[i];
}
}
return sum;
}
console.log(sumPair([1, 6, 100, 346, 761, 249]));
console.log(sumPair([2, 4, 9, 73]));
function sumOrChickPair(input) {
if (Array.isArray(input) === true) {
let sum = 0;
for (let i = 0; i < input.length; i++) {
if (input[i] % 2 === 0) {
sum += input[i];
}
}
return sum;
} else if (Array.isArray(input) === false) {
let chick = "";
for (let i in input) {
if (input[i] % 2 === 0) {
chick = chick.concat(i + " & ");
}
}
chick = chick.substring(0, chick.length - 2);
return chick.concat("have pair values");
}
}
obj = {
a: 2,
b: 5,
c: 8,
};
console.log(sumOrChickPair(obj));
console.log(sumOrChickPair([1, 6, 100, 346, 761, 249]));
console.log(sumOrChickPair([2, 4, 9, 73]));
/**
* 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"
*
*/