Chapter 2 Exercises
Looping a triangle
I tried this three different ways (after Ivan’s demonstration), as I thought it unnecessary to use two for loops for the task.
Triangle (by nested ‘for’ loops)
var num_rows = 7;
for(var row=0;row<num_rows;row++){
var toPrint="#";
for(var column=0; column<row; column++){
toPrint += "#";
}
console.log(toPrint);
}
Triangle (by While loop)
I thought I might be able to make this simpler and wanted to try the other looping methods, so here is my (while loop) version. Also, in the book the writer gave a hint to use the length attribute of the variable!
var maxLength=7;
var toPrint="#";
while (toPrint.length<maxLength+1){
console.log(toPrint);
toPrint+="#";
}
Triangle (by Do loop)
var maxLength=7;
var toPrint="#";
do {console.log(toPrint);
toPrint+="#";
}
while (toPrint.length<maxLength+1);
Triangle (in an eloquent ‘for’ loop)
I was reasonably satisfied with the ‘Do’ loop effort, but I wondered if I could improve on the original double ‘for’ loop.
for (let toPrint="#";toPrint.length <= 7; toPrint +="#"){
console.log(toPrint);
That was pretty nice, and later I found it was virtually the same as the author’s solution, which was.
for (let line = "#"; line.length < 8; line += "#")
console.log(line);
FizzBuzz
FizzBuzz version 1
var maxnum = 100;
var num = 1;
var A = 3; // Divisible by A.
var B = 5; // Divisible by B and not by A.
printA = "Fizz";
printB = "Buzz";
do { if (num % 3 == 0) { console.log(printA);
} else if (num % 5 == 0) {console.log(printB);
} else {console.log(num);
} num++;}
while (num <= maxnum);
That worked on the first try, now to do the modified version.
var maxnum = 100;
var num = 1;
var A = 3; // Divisible by A.
var B = 5; // Divisible by B and not by A.
printA = "Fizz";
printB = "Buzz";
printC = "FizzBuzz"; // If num is divisible by both A and B.
do { if (num % 3 == 0 && num % 5 == 0) { console.log(printC);
} else if (num % 3 == 0) { console.log(printA);
} else if (num % 5 == 0) {console.log(printB);
} else {console.log(num);
} num++;}
while (num <= maxnum);
The writer’s version (below) was even shorter! I noticed that he likes to use inline ‘let’ statements rather than declaring ‘var’ before everything else.
for (let n = 1; n <= 100; n++) {
let output = "";
if (n % 3 == 0) output += "Fizz";
if (n % 5 == 0) output += "Buzz";
console.log(output || n);
}
Chessboard (Failed attempt!)
This failed in the do … while loop!
(Printing the variables was just for testing.)
var black = "#"; console.log("black is "+ black);
var white = " "; console.log("white is " + white);
var gridWidth = 8; console.log("gridWidth is " + gridWidth);
var gridHeight = 8; console.log("gridHeight is " + gridHeight);
var newLine = "\n"; console.log("newLine is " + newLine);
var grid = white; console.log("grid is \"" + white + "\"");
var nextSpot = black; console.log("nextSpot is " + nextSpot);
var gridLength = gridWidth * gridHeight; console.log("gridLength is " + gridLength);
do { grid = grid + nextSpot;
if ((grid.length + 1) % gridWidth == 0) {nextSpot=newLine;}
else if (nextSpot==black) {nextSpot=white;}
else if (nextSpot==white) {nextSpot=black;} }
while (grid.length < (gridLength + 1));
console.log(grid);
I had been trying to make sure that at each gridWidth the next character would be the newLine, but my loop wouldn’t work! I was pretty sure that the addition of the newlines would mess up the string length max, I figured that I would adjust for that after I got a somewhat working model. I got that slight headache that the author suggest was excuse enough to look at his answer!
Here is the author’s solution. I had to stare at it for a while to realise that the newline came in in the OUTER loop.
Part of my problem may have been that I had been trying to do everything in a single loop.
let size = 8;
let board = "";
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if ((x + y) % 2 == 0) {
board += " ";
} else {
board += "#";
}
}
board += "\n";
}
console.log(board);