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

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

What I Changed

When appending the values into alphabet list, the original code was only appending i with was te increment variable. I changed it by making the function append alphabetList[i] so that each value of the alphabet was pushed rather than its index. I also changed the max value of i to be less than 26 so that all 26 letters were appended rather than only 10

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[i-1] , " is letter number 5 in the alphabet")
	}
}

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

What I Changed

When the for loop was written i< alphabetList which is a list not a number. To fix it I changed it to alphabetList.length to add a number. I also changed alphabetList[i] to alphabetList[i-1] to prevent an indexing error since alphabetList index starts from 0 while letterNumber starts from 1.

Segment 3: Odd Numbers

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

Code:

%%js

let odd = [];
let i = 1;

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

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

What I Changed

The whole program had the even list instead of the odd list so I replaced it with odd. I also made 1 the starting point as that was the first odd number for below 10.

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) 


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 burger_count = prompt("How many Burgers")
total += menu.burger * burger_count
var fries_count = prompt("How many Fries")
total += menu.fries *fries_count
var drink_count = prompt("How many Drinks")
total += menu.drink * drink_count

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


<IPython.core.display.Javascript object>

What I did Hacks

I added a user input place where the user could input how many of each food on the menuthey wanted and I used it to find the total value