Last active
April 3, 2019 16:31
-
-
Save YozhEzhi/81cc7abec4b1cdb59b4800ede867a79b to your computer and use it in GitHub Desktop.
Алгоритмы. fizzBuzz.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Classical task. | |
| * | |
| * 3 - Fizz | |
| * 5 - Buzz | |
| * 15 - FizzBuzz | |
| */ | |
| function fizzBuzz() { | |
| let result; | |
| for (let i = 1; i < 31; i++) { | |
| const isFizz = i % 3 === 0; | |
| const isBuzz = i % 5 === 0; | |
| const result = (isFizz && isBuzz) ? | |
| 'FizzBuzz' : | |
| isFizz ? 'Fizz' : | |
| isBuzz ? 'Buzz' : i; | |
| console.log(result); | |
| } | |
| } | |
| fizzBuzz(); | |
| /** | |
| * Write a function that takes an integer and returns an array [A, B, C], | |
| * where A is the number of multiples of 3 (but not 5) below the given integer, | |
| * B is the number of multiples of 5 (but not 3) below the given integer and C is the number | |
| * of multiples of 3 and 5 below the given integer. | |
| * For example, solution(20) should return [5, 2, 1] | |
| */ | |
| function solution(number) { | |
| let result = [0, 0, 0]; | |
| for (let i = 1; i < number; i++) { | |
| const isFizz = i % 3 === 0; | |
| const isBuzz = i % 5 === 0; | |
| const incrementByIndex = index => (result[index] || 0) + 1; | |
| (isFizz && isBuzz) ? | |
| result[2] = incrementByIndex(2) : | |
| isFizz ? result[0] = incrementByIndex(0) : | |
| isBuzz ? result[1] = incrementByIndex(1) : null; | |
| } | |
| return result; | |
| } | |
| // Smaller variant: | |
| function solution(number) { | |
| --number; | |
| const C = Math.floor(number / 15); | |
| const A = Math.floor(number / 3) - C; | |
| const B = Math.floor(number / 5) - C; | |
| return [c3, c5, c15]; | |
| } | |
| console.log(solution(20)); // [5, 2, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment