Optimize fizzbuzz
This commit is contained in:
parent
4581f41517
commit
58ffcec09b
|
@ -17,14 +17,10 @@ console.log('PART 3')
|
|||
|
||||
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
|
||||
|
||||
/* A function that returns eplekake, eple, kake or n based on what n can be divided by */
|
||||
const eplekakeCheck = (n) => {
|
||||
/* if it's divisible by 15 (which is 3*5), it's also divisible by both 3 and 5 */
|
||||
if (i % 15 == 0) return 'eplekake';
|
||||
else if (i % 3 == 0) return 'eple';
|
||||
else if (i % 5 == 0) return 'kake';
|
||||
else return String(n);
|
||||
}
|
||||
/* A function that returns eple and/or kake if it's divisible by 3 or 5, but defaults
|
||||
* to n if it's not (an empty string combined with || will return the latter element)
|
||||
*/
|
||||
const eplekakeCheck = (n) => (n % 3 ? '' : 'eple') + (n % 5 ? '' : 'kake') || n;
|
||||
|
||||
/* Print the output of eplekakeCheck for each element in numbers */
|
||||
for (i of numbers) {
|
||||
|
|
Loading…
Reference in New Issue