The quickest method i can see is to use the For statement
// Shorthand version
for (let hashstring="#";hashstring.length<8;hashstring +="#"){ console.log(hashstring); }
// Longhand version
var hashstring = “#”; for(i=0;hashstring.length<7;i++){
console.log(hashstring);`
hashstring += “#”;
}
You can also carry out the some solution using a While loop as follows -
var hashstring = “#”;
while(hashstring.length<7){
console.log(hashstring);
hashstring += “#”;
}
And also a do…while loop as follows -
var hashstring = “#”;
do{
console.log(hashstring);
hashstring += “#”;
}
while (hashstring.length < 7);
Fizz Buzz
First solution that creates the output of fizz OR Buzz or Number using if…else statements
for(i=1;i<101;i++){
if(i%3===0){
console.log("fizz ");
}else if(i%5===0){
console.log("buzz ");
}else
{
console.log(i);
}
}
I have used 2 methods of the fizzbuzz solution as follows, the first uses if…else statements
for(i=1;i<101;i++){
if(i%3===0){
if(i%5===0){
console.log("fizzBuzz ");
}else {
console.log("fizz ");
}
}else if(i%5===0){
console.log("buzz ");
}else{
console.log(i);
}
}
The second method uses the “continue” method to restart the loop at the next iteration once a statement is true.
for(i=1;i<101;i++){
if (i%3===0 && i%5===0){console.log(“FizzBuzz”);continue;}
if (i%3===0){console.log(“Fizz”);continue;}
if (i%5===0){console.log(“Buzz”);continue;}
console.log(i);
}
Chessboard
For the Chessboard excersize i have used nested loops and if…else statements to check if the line number is odd or even to change the order of the spaces and # characters, changing the variable “boardsize” value allows to make chessboards of different required sizes.
var boardsize=8;
for(i=0;i<boardsize;i++){
for(j=0;j<boardsize;j++){
if(i%2===0){
if(j%2===0){boardoutput +=" “; }else{boardoutput +=”#";}
if(j===boardsize-1){boardoutput +="\n";}
}else{
if(j%2===0){boardoutput +="#"; }else{boardoutput +=" “;}
if(j===boardsize-1){boardoutput +=”\n";}
}
}
}
console.log(boardoutput);