Triangle
for(var line= "#"; line.length < 8; line+="#"
)
console.log(line);
- < 8 means that the lengt will not exeed 8, cant be more than 8 bits. line += means, “#” will be added each loop to the variable
- give line the string varaiable “#”, the .lenght counts how many letters it is in the variable,*
FizzBuzz
for (var a = 1; a <=100; a++) {
if (a%3 === 0 && a%5 === 0){
console.log("FizzBuzz")
}
else if (a%3 === 0) {
console.log("Fizz")
}
else if (a%5 === 0) {
console.log("Buzz")
}
else {
console.log(a)
}
If var a is diversible with 3 AND 5, logg FizzBuzz
else, if var a is diversible with 3, logg Fizz
else, if var a is diversible with 5, logg Buzz.
else, console log a (which is the numbers in the loop of 1 to 100)
ChessBoard
let size = 8; *//how many steps it can max go to the side and down*
let board = ""; *//an empty line that will output a space, # or a new line*
//*the two for loops underneath are embedded within eachother*
for(let a = 0; a < size; a++) { `// for loop the vertical row`
for(let b = 0; b < size; b++) {` //for loop the horisontal row`
if ((a+b) % 2 == 0){ // when we set a+b every second number divisible by 2 will execute the space character "" the loop goes 1,space,3,space,5,space,7,space
board += " ";
} else {
board += "#"; //if not divisivble with 2, print #
}
}
board += "\n"; //new line: board +="\n" is the same as board = board + "\n", attached to the for loop that contains the b
}
console.log(board); //logging the final form of the board variable