Debugging Arithmetic Expressions

The program below starts from a manuscript of 1427 pages, split into chapters of 36 pages. It should print the full chapters, the pages left for a short last chapter, and whether that chapter has at least 24 pages. Then it should print whether the pages fill whole chapters, and last the thousands digit. This lesson finds the program's mistakes and repairs them. Run the code below. As the lesson reaches each mistake or change, make it in the editor and run the code again.
let pages = 1427;                          // pages in the manuscript
let chapters = Math.floor(pages / 36);     // full chapters
let left = pages % 36;                     // pages in the short last chapter
let keepLast = pages >= 24;                // true if the last chapter has 24+
console.log(chapters, left, keepLast);
let evenSplit = pages % 36 === 36;         // true if no pages are left over
console.log(evenSplit);
console.log(pages % 1000);                 // the thousands digit
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.

Logic errors

The program runs to its last line and prints these three lines:
39 23 true
false
427
The correct output for 1427 pages is 39 chapters, 23 pages left, and false, then false, then 1. Two of the five values are wrong, and JavaScript gave no error message.
A syntax error keeps a program from starting at all, and a run-time error halts it partway through. This program ran to the end, so its wrong values come from logic errors.

Three kinds of division

Read each line that divides against what it should compute: a count of whole things, a leftover, a digit, or a yes-or-no. JavaScript has no whole-number division operator, so the chapters line counts with Math.floor. It rounds the quotient down to 39, the number of whole 36s in 1427. The next line's % gives 23, the pages left over. Both lines are right.
A line that wanted the fraction, such as the manuscript's length in chapters as a decimal, would use / alone: pages / 36 is 39.638888888888886.

Carrying the leftover

The fourth line should ask whether the 23 pages left over make a last chapter of at least 24 pages. They don't, so it should print false. It tests pages instead, and 1427 is at least 24, so it prints true. It's true for any manuscript of 24 pages or more.
The question is about what the % line left, so that leftover has to be carried into the next step. The line becomes let keepLast = left >= 24;, which gives false.

A yes-or-no line

The evenSplit line should be true when the pages fill whole chapters with none left over. For 1427 it printed false, which is correct, since 23 pages are left. One correct output doesn't make a line correct, though.
A manuscript of 1440 pages is exactly 40 chapters, so the line should give true. It gives false, because 1440 % 36 is 0, and 0 isn't 36. The count can't make this test either: Math.floor(1440 / 36) is 40 and Math.floor(1427 / 36) is 39, and both are whole numbers.
So the old line was false for every page count. It becomes let evenSplit = pages % 36 === 0;, which gives false for 1427 and true for 1440.

Reading off digits

The last line should print the thousands digit of 1427, which is 1, but it printed 427. Dividing by 1000 splits a number before its last three digits, and % keeps those three. That's the 427 the line printed.
The thousands digit is in the part that Math.floor keeps, and for a four-digit number that part is the thousands digit alone. The repair divides by 1000 and rounds down, Math.floor(pages / 1000), which gives 1.

Typed input

Next the page count is typed in rather than written into the code. Now let pages = prompt("Pages: "); reads it, and one more statement at the end shows the count after a 16-page index is added, console.log(pages + 16);. Typing 1427 prints 39 23 false, false, and 1, the repaired values, and then 142716.
The earlier lines still work because / and % convert the text "1427" to a number. The last line's + joins the text to 16 instead, again with no error message.
The repair converts once, where the count is read: let pages = Number(prompt("Pages: "));. Every later line then gets a number, and the last line prints 1443. A typed comma, as in 1,427, is text that Number() can't read, so it makes pages NaN.

Testing the repaired program

The page counts 1427, 1430, and 1440 are defined inputs: each was chosen with the output it should produce worked out by hand.
Here is the program with all four repairs.
let pages = Number(prompt("Pages: "));
let chapters = Math.floor(pages / 36); // full chapters
let left = pages % 36; // pages in the short last chapter
let keepLast = left >= 24; // true if the last chapter has 24+
console.log(chapters, left, keepLast);
let evenSplit = pages % 36 === 0; // true if no pages are left over
console.log(evenSplit);
console.log(Math.floor(pages / 1000)); // thousands digit if pages < 10000
console.log(pages + 16); // with a 16-page index added
Typing 1427 prints 39 23 false, false, 1, and 1443, all correct. Typing 1430, which leaves 26 pages for the last chapter, prints 39 26 true, false, 1, and 1446. Typing 1440 prints 40 0 false, true, 1, and 1456, as expected for exactly 40 chapters. Testing with 1427 alone would have passed the old evenSplit line, which also gave false for it. A yes-or-no line should be tested with a defined input for each answer it can give.