Skip to content

Commit

Permalink
Create fizzbuzz_if_else_and_switch_states.js
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DesLandysh authored Jun 11, 2022
1 parent 260e5cf commit c54dd06
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions fizzbuzz_if_else_and_switch_states.js
Original file line number Diff line number Diff line change
@@ -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);
};
}

0 comments on commit c54dd06

Please sign in to comment.