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 < 10; i++) {
	alphabetList.push(alphabet[i]);
}

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

What I Changed

I edited the code so that it pushes the letters from the string in the variable “alphabet” instead of just pushing just the number in variable i.

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 x = 1
for (var i = 0; i < 10; i++) {
	alphabetList.push(alphabet[i]);
	
	console.log( alphabetList[i] + " is letter number " + x +" in the alphabet")
	x += 1
}

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

// Copy your previous code to built alphabetList here
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];
var x = 0
for (var i = 0; i < 10; i++) {
	alphabetList.push(alphabet[i]);
	x += 1
    if (i == 4) {
        console.log( alphabetList[i] + " is letter number " + x +" in the alphabet")

    }
	
}

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

What I Changed

First, I created a variable ‘x’ with the starting value of 1 which increases in the for loop by +1. I changed so that prints out the ‘i’ term in the list(alphabetList) then prints “ is letter number” and then prints out the value in x (the positions of the alphabet) and finally prints “ in the alphabet” if i = 4

Segment 3: Odd Numbers

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

Code:

%%js

let evens = [];
let i = 1;

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

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

What I Changed

I changed the code so that instead of printing out all of the even numbers below 10, it prints out all of the odd numbers. I did this by editing the starting value of i to 1 instead of 0.


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?
    • % finds the reminder, and in the code, the reminder was set to 1 instead of 0.
  • Make changes to get the intended outcome.
%%js

var numbers = []
var numbers_none = [] 
var new_numbers = []
var i = 0
while (i<100) {
    numbers.push(i)
    i += 1
}
for (var i of numbers) {
    if (numbers [i] % 5 == 0 ) {
        new_numbers.push(numbers[i])
    } else {
        if (numbers [i] % 2 ==0) {
            new_numbers.push(numbers[i])
        } else {
            numbers_none.push(numbers[i])
        }
        
    }
}
console.log(new_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 x = 0;
var item = ["burger", "fries"];
var total = 0;

for (let i = 0; i < item.length; i++) {
    total += menu[item[i]];
    console.log("Running total: $" + total.toFixed(2));
}

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


<IPython.core.display.Javascript object>

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)