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

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

What I Changed

I changed i in alphabetList.push to alphabet[i] so it printed the string of letters instead of the letters coresponding values.

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

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

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

console.log(alphabetList);

let letterNumber = 5; //change this to whatever letter you want.

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

<IPython.core.display.Javascript object>

What I Changed

Added the previous code to the new code and then changed it so that it would instead of printing letter 5 it would print the letter coresponding to the variable.

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

I changed all the evens variables to odds, and then changed starting # to an odd #.

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 = []; // Create an empty array
var newNumbers = []; // Create an empty array
var i = 0; // Set the counter to 0

while (i < 100) { // While the counter is less than 100
    numbers.push(i);
    i += 1;
}

for (var number of numbers) { // For each number in the array
    if (number % 5 === 0 || number % 2 === 0) { // If the number is divisible by 5 or 2
        if (!newNumbers.includes(number)) { // Check if the number is not already in newNumbers
            newNumbers.push(number); // Add the number to the new array
        }
    }
}

console.log(newNumbers); // Print the new array



<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} //menu items and prices
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"

if (menu[item]) {
    total += menu[item]; // Add the price of the selected item to the total
    console.log("Added " + item + " to your order.");
} else {
    console.log("Invalid item selected.");
}

console.log("Total: $" + total.toFixed(2)); // Displays the total price with two decimal places

<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)