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 what was being pushed. Before, the numbers were being pushed, but now, the corresponding alphabet index is being pushed.

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

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

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

What I Changed

I changed

  • for loop said “i < alphabetList”
    • This wouldn’t work as alphabetList is a list and can’t be compared to an integer
  • If statement w/ 3 equal signs
    • To compare two variables, a double equal sign must be used (==)
  • Printed alphabetList[letterNumber - 1] instead of letterNumber
    • We have to index the letter from the alphabet list to get the letter, we can’t just put the index of the number
    • The index must be subtracted by 1 as the index starts at 0, so the letter ‘e’ is at index 4 and not 5

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…

  • What the output was
    • The output was originally getting all even nums to output, so I switched the var names
    • I changed the initial starting value of the number i. By changing it from 0 to 1, we are able to get the odd values and not the evens

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 = []
var i = 0

while (i < 100) {
    numbers.push(i)
    i += 1
}
for (var i of numbers) {
    if (numbers[i] % 5 === 0)
        newNumbers.push(numbers[i])
    if (numbers[i] % 2 === 0)
        newNumbers.push(numbers[i])
}
console.log(newNumbers) 


<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? Used for decimal places, very cool
}
//ideally the code should support mutliple items (I sense a misspelling in this sentence, do you?)
var keep_going = 'N';
var order_list = {
    '1': 0,
    '2': 0,
    '3': 0
};
while (keep_going == 'N') {
    order_list[prompt("Would you like a burger (1), some fries (2), or a drink (3)?]")] += parseInt(prompt("How many would you like?"))
    var keep_going = prompt("Is that it for today? [Y/N]").toUpperCase() //the toUpperCase makes sure a lowercase y or n is still accepted
}


//code should add the price of the menu items selected by the user 
total += menu['burger'] * order_list['1']
total += menu['fries'] * order_list['2']
total += menu['drink'] * order_list['3']

total *= 1.0725
var tip = parseInt(prompt("Please put the amount in $ that you would like to tip: "))

console.log('$' + total.toFixed(1) + tip)
<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)