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(i);
}

console.log(alphabetList);
<IPython.core.display.Javascript object>

What I Changed

I changed the i < 10 statement to i < alphabet.length. This is because if i is simply lesser than 10, the code will print a statement saying there are only 10 alphabets (counting from 0). If i is lesser than the alphabet string length, then it will print that the alphabet has 26 letters.

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

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

for (var i = 0; i < alphabet.length; i++) {
	alphabetList.push(alphabet[i]);
	letterNumber += 1
	
	if (i == 4) {
		console.log(alphabetList[i] + " is letter number " + letterNumber + " in the alphabet.")
	}
}

console.log(alphabetList);

// Should output:
// "e" is letter number 5 in the alphabet
<IPython.core.display.Javascript object>

What I Changed

I changed the value of the variable letterNumber from 5 to 0. In doing so, I was able to print out all the letters from a-z with their corresponding positions by using alphabetList.push(alphabet[i]) and increasing letterNumber by 1 everytime the loop ran. Now, by further restricting what is printed using an if statement to guarantee that i == 4, I was able to only print that e is letter number 5 in the alphabet.

Segment 3: Odd Numbers

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

Code:

%%js

let odds = [];
let i = 1;

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

console.log(odds);
<IPython.core.display.Javascript object>

What I Changed

To begin, I let odds and not evens = the array. Then, I changed the value of i from i = 0 to i = 1. This is because when the while loop runs, 2 is added to i until i <= 10. Since i is now starting from 1 and not 0, adding 2 until i <= 10 would mean that we are obtaining odd numbers less than or equal to 10 and not even numbers less than or equal to 10 now.

(ADDITIONAL) Segment 4:

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?
    • The values that are outputted incorrectly are the repeated values of the multiples of 2 and 5. When both 2 and 5 are common multiples for a number, they print twice because the if statement is not set up properly.
  • Make changes to get the intended outcome.
    • [SEE BELOW].
%%js

var numbers = [];
var invalid_numbers = [];
var divisible_numbers = [];
var i = 0;

while (i <= 100) {
    numbers.push(i);
    i += 1;
}
 for (var i of numbers) {
    if (numbers[i] % 5 == 0){            // We look for divisibility by 5 here
        divisible_numbers.push(numbers[i])     // If the number is not divisible by 5, then we enter an 'else' statement that looks for divisibility by 2
    } else {                             // This gets rid of duplicate numbers because we are not printing both sets of numbers that satisfy divisbility by 2 and 5 as well
        if (numbers [i] % 2 == 0) {
            divisible_numbers.push(numbers[i])
        } else {
            invalid_numbers.push(numbers[i])
        }
    }
        
    
 }

 console.log(divisble_numbers)
<IPython.core.display.Javascript object>

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

//shows the user the menu and prompts them to select an item
console.log("Menu")
for (var item in menu) {
    console.log(item + "  $" + menu[item].toFixed(2)) //why is toFixed used?
}
//ideally the code should support mutliple items
var item = "burger"

//code should add the price of the menu items selected by the user 
console.log(total)

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)