-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter 2
49 lines (41 loc) · 1.12 KB
/
Chapter 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Q-1: Write a loop that makes seven calls to console.log to output the following triangle:
#
##
###
####
#####
######
#######
Sol:
for(let i = 1, line = "#"; i <= 7; i++, line += "#"){
console.log(line);
}
Q-2: 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.
Sol:
for(let i = 1; i < 101; i++){
if(i % 3 == 0 && i % 5 == 0)
console.log("FizzBuzz");
else if(i % 5 == 0)
console.log("Buzz");
else
console.log(i);
}
Q-3: 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:
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
Sol:
let char = " ";
for(let i = 0; i < 8; i++){
for(let j = 0; j < 8; j++){
char += (i+j) % 2 == 0 ? "#" : " ";
}
char+="\n";
}
console.log(char);