Optimize fizzbuzz

This commit is contained in:
Oystein Kristoffer Tveit 2020-09-24 12:53:20 +02:00
parent 4581f41517
commit 58ffcec09b
1 changed files with 4 additions and 8 deletions

View File

@ -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] 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 */ /* A function that returns eple and/or kake if it's divisible by 3 or 5, but defaults
const eplekakeCheck = (n) => { * to n if it's not (an empty string combined with || will return the latter element)
/* 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'; const eplekakeCheck = (n) => (n % 3 ? '' : 'eple') + (n % 5 ? '' : 'kake') || n;
else if (i % 3 == 0) return 'eple';
else if (i % 5 == 0) return 'kake';
else return String(n);
}
/* Print the output of eplekakeCheck for each element in numbers */ /* Print the output of eplekakeCheck for each element in numbers */
for (i of numbers) { for (i of numbers) {