EloquentJS-Program Structure excercise

Looping a triangle

Write a loop that makes seven calls to console.log to output the following triangle:

1
2
3
4
5
6
7
#
##
###
####
#####
######
#######
1
2
3
for(var i=1; i<=7; i++){
console.log("#".repeat(i) + "\n");
}

Chess board

Write a program that creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a “#” character. The characters should form a chess board.

Passing this string to console.log should show something like this:

1
2
3
4
5
6
7
8
 # # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
1
2
3
4
5
6
7
for(var j = 1; j<=8; j++){
var str = ''
for(var i =1; i<= 8; i++){
(i+j)%2 ? str+=" " : str+="#";
}
console.log(str + "\n");
}

FizzBuzz

Write a program that uses console.log to print all the 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.

When you have that working, modify your program to 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).

1
2
3
4
5
6
7
8
9
10
11
12
13
for(var i=1; i<=50; i++){
if(i % 5 ==0 && i % 3 ==0){
console.log("FizzBUZZ");
continue;
} else if (i%5==0){
console.log("Buzz");
continue;
} else if ( i % 3 ==0){
console.log("FIZZ");
continue;
}
console.log(i);
}