Home HTML Data Types DOM JavaScript JS Debugging

College Board Big Idea 1

Identifying and Correcting Errors (Unit 1.4)

Become familiar with types of errors and strategies for fixing them

  • Review CollegeBoard videos and take notes on blog
  • Complete assigned MCQ questions if applicable

Code Segments

Practice fixing the following code segments!

Segment 1: Alphabet List

Intended behavior: create a list of characters from the string contained in the variable alphabet

Code:

%%js

var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];

for (var i = 0; i < alphabet.length; i++) {
	alphabetList.push(alphabet[i]);
}

console.log(alphabetList);

What I Changed

I changed… In the for loop condition, I changed i < 10 to i < alphabet.length to iterate over the entire alphabet string.

Segment 2: Numbered Alphabet

Intended behavior: print the number of a given alphabet letter within the alphabet. For example:

"_" is letter number _ in the alphabet

Where the underscores (_) are replaced with the letter and the position of that letter within the alphabet (e.g. a=1, b=2, etc.)

Code:

%%js

// Copy your previous code to built alphabetList here

// Copy your previous code to build alphabetList here

let letterNumber = 5;

console.log(alphabet[letterNumber - 1] + " is letter number " + letterNumber + " in the alphabet");

What I Changed

I changed…I removed the for loop because it’s unnecessary. You can directly access the alphabet letter at the specified position.

Segment 3: Odd Numbers

Intended behavior: print a list of all the odd numbers below 10

Code:

%%js

let odds = [];
let i = 1; // Start with 1 to get odd numbers

while (i < 10) {
  odds.push(i);
  i += 2;
}

console.log(odds);

What I Changed

I changed… I changed the variable name from evens to odds to reflect that we want to collect odd numbers. I changed the initial value of i to 1 to start with an odd number.

BELOW NOT EDITED

The intended outcome is printing a number between 1 and 100 once, if it is a multiple of 2 or 5

  • What values are outputted incorrectly. Why?
  • Make changes to get the intended outcome.
%%js
var numbers = [];
var newNumbers = [];

for (var i = 1; i <= 100; i++) {
    numbers.push(i);
    if (i % 5 === 0 || i % 2 === 0) {
        newNumbers.push(i);
    }
}

console.log(newNumbers);


Challenge

This code segment is at a very early stage of implementation.

  • What are some ways to (user) error proof this code?
  • The code should be able to calculate the cost of the meal of the user

Hint:

  • write a “single” test describing an expectation of the program of the program
  • test - input burger, expect output of burger price
  • run the test, which should fail because the program lacks that feature
  • write “just enough” code, the simplest possible, to make the test pass

Then repeat this process until you get program working like you want it to work.

%%js
var menu = {
    "burger": 3.99,
    "fries": 1.99,
    "drink": 0.99
};
var total = 0;

console.log("Menu");
for (var item in menu) {
    console.log(item + "  $" + menu[item].toFixed(2));
}

// User's order
var userOrder = ["burger", "fries", "drink"];

// Calculate the total cost of the user's order
for (var i = 0; i < userOrder.length; i++) {
    var selectedItem = userOrder[i];
    if (menu[selectedItem]) {
        total += menu[selectedItem];
    }
}

console.log("Total cost: $" + total.toFixed(2));

Hacks

  • Fix the errors in the first three segments in this notebook and say what you changed in the code cell under “What I Changed” (Challenge is optional)