-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxMod5.js
More file actions
executable file
·32 lines (26 loc) · 862 Bytes
/
maxMod5.js
File metadata and controls
executable file
·32 lines (26 loc) · 862 Bytes
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
// MAXMOD5 from codingbat.com
/*
Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the return the smaller value. However, in all cases, if the two values are the same, return 0. Note: the % "mod" operator computes the remainder, e.g. 7 % 5 is 2.
*/
// Evan Genest, 4/2019, twitter@mistergenest
let maxMod5 = (a, b)=>{
let modDiff = a % 5 - b % 5;
modDiff = Boolean(modDiff);
try {
if (a === b){
return 0;
} else if (!modDiff){
return [a, b].reduce( (a, b)=>{return(a > b)? b : a});
} else if (modDiff){
return [a, b].reduce( (a, b)=>{return(a > b)? a : b});
} else {
throw new Error("Should never see this.");
}
}
catch(e){
console.log(e);
}
}
console.log(maxMod5(2, 3)); // 3
console.log(maxMod5(6, 2)); // 6
console.log(maxMod5(3, 2)); // 3