Debugging List Traversals
The program below keeps a summer reading list as an array of page counts, and
finished holds the page count of a book already read. It should remove that book and every book under 150 pages, then count the places where a book is shorter than the one before it. It runs to the end without an error and prints a wrong answer. This lesson finds its three bugs and repairs them one at a time. Run the program below, and repair each bug in it as the lesson finds it.let pages = [312, 96, 132, 240, 188, 121, 275, 164];
const finished = 212;
pages.splice(pages.indexOf(finished), 1);
for (let i = 0; i < pages.length; i++) {
if (pages[i] < 150) {
pages.splice(i, 1);
}
}
let drops = 0;
for (let i = 0; i < pages.length; i++) {
if (pages[i + 1] < pages[i]) {
drops++;
}
}
console.log(pages, drops);
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.
The finished book
Run as written, the program prints this line.
[ 312, 132, 240, 188, 275 ] 2
Two things are wrong with it. The 164-page book at the end is gone, and nothing asked for its removal. The 132-page book is still there, although it's under 150 pages. The first problem comes from line 3.
The finished book has 212 pages, and no book on this list does. So
pages.indexOf(finished) is -1, and line 3 runs as pages.splice(-1, 1). Since splice counts a negative start back from the end of the array, it removes the last book. JavaScript reports nothing, and the rest of the program runs on an array one book short.Checking before removing
A finished book that isn't on the list leaves nothing to remove, and the program should carry on. The repair asks first with
includes, which searches the array as indexOf does but answers true or false instead of giving a position:if (pages.includes(finished)) {
pages.splice(pages.indexOf(finished), 1);
}With
finished at 212 the test is false, so the list keeps all eight books. On a list that does hold a 212-page book, the test is true and splice removes the first one indexOf finds.Removing inside a loop
With line 3 repaired, a
console.log(pages) placed right after the first loop prints the array below.[ 312, 132, 240, 188, 275, 164 ]
The 132-page book survived, although 132 is less than 150. The loop keeps its place in
i. At position 1 it finds 96, and pages.splice(i, 1) removes it.Every later book shifts one place to the left, so 132 now sits at position 1. Then
i++ sets i to 2, where the loop finds 240, and it never reads 132. The 121-page book follows a book that stayed, so the loop reaches it and removes it.The repair builds a new array of the books to keep, so its test flips to
p >= 150:let longBooks = [];
for (const p of pages) {
if (p >= 150) {
longBooks.push(p);
}
}
pages = longBooks;
This loop reads
pages and never changes it, so every book gets its turn, and the list becomes [ 312, 240, 188, 275, 164 ]. The last line needs pages to be declared with let, since a const name can't be assigned a new value. Looping over pages.slice() and removing each short book from pages with pages.splice(pages.indexOf(p), 1) would also work.Comparing neighbors
The last loop compares each book with the one after it. With both repairs made, the program prints this line.
[ 312, 240, 188, 275, 164 ] 3
The count is right: 240, 188, and 164 are each shorter than the book before them. The loop is still wrong.
The array has positions 0 through 4, and the header gives
i every one of them. When i is 4, pages[i + 1] reads position 5, past the end. JavaScript doesn't stop there.Reading past the end gives undefined, and comparing undefined with a number by <, >, <=, or >= always gives false. So
undefined < 164 adds nothing to drops, and the count comes out right by luck. Looping over every position is the usual slip, since n items make only n − 1 neighboring pairs.For the five books the header becomes
for (let i = 0; i < pages.length - 1; i++), and i runs from 0 to 3. The repaired program prints [ 312, 240, 188, 275, 164 ] 3, and the count now comes from four comparisons of real books. The output is the same before and after the repair, so the output alone couldn't show this bug.