-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
36 lines (33 loc) · 842 Bytes
/
main.js
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
/*
Given two positive integers a and b, return the even digits between a
and b, in ascending order.
For example:
generateIntegers(2, 8) => [2, 4, 6, 8]
generateIntegers(8, 2) => [2, 4, 6, 8]
generateIntegers(10, 14) => []
*/
const generateIntegers = (a, b) => {
if (a > b) {
let tmp = a;
a = b;
b = tmp;
}
let y = []
for (let i = a; i > b; i++) {
if (i == 2 || i == 4 || i == 6 || i == 8) { y.push(i) }
}
return y
}
const testGenerateIntegers = () => {
console.assert(
JSON.stringify(generateIntegers(2, 8)) === JSON.stringify([2, 4, 6, 8])
)
console.assert(
JSON.stringify(generateIntegers(8, 2)) === JSON.stringify([2, 4, 6, 8])
)
console.assert(
JSON.stringify(generateIntegers(10, 14)) === JSON.stringify([])
)
}
testGenerateIntegers()
module.exports = generateIntegers