Created
January 5, 2019 22:57
-
-
Save svngoku/f76405f2a2bc29497bf85ef8088e11a9 to your computer and use it in GitHub Desktop.
EloquentJavaScriptExerciceLoop π created by svngoku - https://repl.it/@svngoku/EloquentJavaScriptExerciceLoop
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
| /* First exercice of Eloquent JavaScript Book about loop */ | |
| /* Looping a Triangle */ | |
| console.log("********* Triangle Loop *********") | |
| function triangle(value) { | |
| let t = "#"; | |
| while(t.length <= value) { | |
| console.log(t); | |
| t += "#"; | |
| } | |
| } | |
| triangle(7); | |
| /* FizzBuzz */ | |
| console.log("********* FizzBuzz Cases *********") | |
| /* Case 1 : printed all numbers from 1 to 100 with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible | |
| by 5 (and not 3), print "Buzz" instead. */ | |
| function fizzbuzzcase1() { | |
| for(let v = 1 ; v < 101 ; v++) { | |
| if(v % 3 == 0 ) { | |
| console.log("Fizz") | |
| } else if (v % 5 == 0) { | |
| console.log("Buzz") | |
| } else { | |
| console.log(v); | |
| } | |
| } | |
| } | |
| fizzbuzzcase1() | |
| /* Case 2 : print "FizzBuzz" for numbers that are divisible by both 3 and 5 | |
| (and still print "Fizz" or "Buzz" for numbers divisible by only one of those) | |
| */ | |
| function fizzbuzzCase2(value) { | |
| if (value % 3 == 0 && value % 5 == 0) { | |
| console.log("FizzBuzz"); | |
| } else if(value % 3 == 0) { | |
| console.log("Fizz") | |
| } else if(value % 5 == 0) { | |
| console.log("Buzz") | |
| } else { | |
| console.log(`La valeur est ${value}`) | |
| } | |
| } | |
| fizzbuzzCase2(2) | |
| /* Chessboard */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment