From c54dd062b99dab3638f5e156c275c4321c185995 Mon Sep 17 00:00:00 2001 From: Des Kitten <60898828+DesLandysh@users.noreply.github.com> Date: Sat, 11 Jun 2022 15:19:14 +0300 Subject: [PATCH] Create fizzbuzz_if_else_and_switch_states.js That the general fizzbuzz, but I tried to write with switch statement, and suddenly discovers for my self the difference between for-of-loop and for-in-loop, that gives values and indexes respectively. Also I create a range generator for an custom length array. --- fizzbuzz_if_else_and_switch_states.js | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 fizzbuzz_if_else_and_switch_states.js diff --git a/fizzbuzz_if_else_and_switch_states.js b/fizzbuzz_if_else_and_switch_states.js new file mode 100644 index 0000000..3621b22 --- /dev/null +++ b/fizzbuzz_if_else_and_switch_states.js @@ -0,0 +1,46 @@ +// Python-like print function +const print = a => console.log(a); + +// return array filled from 1 to desired number +const make_arr = (number) => { + let array = []; + array[number] = undefined; + array = Array.from(array.keys()); + array.shift(); + return array; +}; + +arr = make_arr(20); +// output: [1, 2, 3... ...20] +// print(arr); // to make sure of this + +// FOR OF takes values +for (let i of arr) { + switch (true) { + case (i % 3 === 0 && i % 5 === 0): + print('fizzbuzz'); + break; + case (i % 5 === 0): + print('buzz'); + break; + case (i % 3 === 0): + print('fizz'); + break; + default: print(i); + }; +} + +print('\n') + +// FOR-IN takes indexes +for (let j in arr) { + if (j % 3 === 0 && j % 5 === 0) { + print('fizzbuzz'); + } else if (j % 3 === 0) { + print('fizz'); + } else if (j % 5 === 0) { + print('buzz'); + } else { + print(j); + }; +}