Loop Conditions and Off-by-One Errors
The program below is meant to count the layers a 3D printer needs to reach a height typed at the prompt. Each pass of the loop adds one layer, 0.4 mm thick. This lesson runs it on several inputs and repairs each fault the runs expose.
let target = prompt("Print height in mm: ");
let printed = 0;
let layers = 0;
while (printed !== target) {
printed = printed + 0.4;
layers = layers + 1;
}
console.log("Layers: " + layers);When the condition is checked
Type 2 at the prompt. Since
prompt() returns the typed text as a string, target holds "2". The strict !== counts a number and a string as different whatever they hold, so printed !== target is true at every check.Here no pass can make it false. Adding 0.4 changes the value of
printed but not its type, so the loop never ends.Text from prompt()
target + 0.4 is "20.4", since + joins text, while target - 0.4 is 1.6, since - converts the text to a number first.The loose
!= converts the text too, before comparing, and five layers make exactly 2, so printed != target ends the loop at Layers: 5. The condition printed < target converts too and prints the same. Both loops work by accident. Switching operators has hidden the mistake without repairing it: target is still a string.Decimal values
The repair goes on the first line:
let target = Number(prompt("Print height in mm: "));. With 2 typed, the !== loop now ends after five layers, but only because five additions of 0.4 give exactly 2. With 1.2 typed it never ends, since three layers give 1.2000000000000002.With
printed < target as the condition, 1.2 gives Layers: 3, since 1.2000000000000002 isn't less than 1.2.Testing at the boundaries
An off-by-one error shows at the inputs where the layer count changes. Eight layers make exactly 3.2 mm, so an input of 3.2 should give
Layers: 8. It gives Layers: 9, one layer too many, because eight additions of 0.4 make 3.1999999999999997, which is less than 3.2.JavaScript's number type holds whole numbers of this size exactly, so the repair counts micrometers, 400 to a layer.
Math.round keeps the goal whole, since a decimal times 1000 can itself be off: 2.01 * 1000 is 2009.9999999999998.let target = Number(prompt("Print height in mm: "));
let goal = Math.round(target * 1000);
let printed = 0;
let layers = 0;
while (printed < goal) {
printed = printed + 400;
layers = layers + 1;
}
console.log("Layers: " + layers);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Now 3.2 gives
Layers: 8, and 3.3, just past it, gives Layers: 9. The smallest height, 0, gives Layers: 0, since the condition is false at the first check. Run the program above and type 3.2, then run it again with 3.3 and with 0.One pass too soon
A print job can hold several pieces printed one after another, each with its own height. Counting all their layers takes a loop inside a loop.
let pieces = Number(prompt("Pieces: "));
let layers = 0;
let printed = 0;
let piece = 1;
while (piece < pieces) {
let target = Number(prompt("Print height in mm: "));
let goal = Math.round(target * 1000);
while (printed < goal) {
printed = printed + 400;
layers = layers + 1;
}
piece = piece + 1;
}
console.log("Layers: " + layers);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Given 3 pieces, of 2.8, 1.2, and 2 mm, it should print
Layers: 15 (7, 3, and 5). It asks for only two heights and prints Layers: 7. Run the program above with those inputs. As the lesson finds each mistake below, repair it in the program and run it again.Since
piece starts at 1, piece < pieces turns false when piece reaches 3, so the outer loop runs only twice. That comparison is one short: piece <= pieces was meant.The second piece adds no layers either. The line
let printed = 0; sets the counter once, before the outer loop, so printed is still 2800 when the goal becomes 1200.So
let printed = 0; belongs inside the outer loop, and let layers = 0; stays above it, because the last line reads layers. With both repairs the program prints Layers: 15.Flags
A loop can also be driven by a flag, a variable that holds
true or false, instead of a comparison in the while line. In the micrometer version the loop becomes:let printing = true;
while (printing) {
printed = printed + 400;
layers = layers + 1;
if (printed >= goal) {
printing = false;
}
}
An input of 4 still gives
Layers: 10, but the boundary test at 0 gives Layers: 1. The goal is compared only after a layer has been added, so the first layer always prints. Starting the flag as let printing = printed < goal; repairs it: at 0 the flag starts false, and the body never runs.