Skip to content

Instantly share code, notes, and snippets.

@svngoku
Created January 5, 2019 22:57
Show Gist options
  • Select an option

  • Save svngoku/f76405f2a2bc29497bf85ef8088e11a9 to your computer and use it in GitHub Desktop.

Select an option

Save svngoku/f76405f2a2bc29497bf85ef8088e11a9 to your computer and use it in GitHub Desktop.
EloquentJavaScriptExerciceLoop πŸ” created by svngoku - https://repl.it/@svngoku/EloquentJavaScriptExerciceLoop
/* 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