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";
// this lists all the letters of the alphabet for the list to use.
var alphabetList = [];
// we define the list as a variable to use later.

for (var i = 0; i < alphabet.length; i++) {
    alphabetList.push(alphabet[i]);
}
// we marked the seperate letter to be identified as a numerical value
console.log(alphabetList);

What I Changed

The code was running through numbers 0-9 which made this a quick fix to ensure that the code runs the values all the way through 26.

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 = [];

let letterNumber = 5

for (var i = 0; i < alphabet.length; i++) {
	if (i === letterNumber) {
		console.log(" e is letter number 5 in the alphabet")
	}
}

// Should output:
// "e" is letter number 5 in the alphabet

What I Changed

The code needed a alphabet variable to function. Another change was making i less than 26(number of letters in the alphabet) because 0 counts as a value. I also changed the message to what was intended.

Segment 3: Odd Numbers

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

Code:

js

let odds = [];
let i = 1;
// the first odd number would be 1 as the starting value

while (i < 10) { 
  // the function will continue as long as the number is less than 10
odds.push(i);
  i += 2; 
  // increasing by two ensures that the function will go to the next odd number
}

console.log(odds);

What I Changed

While doing this challenge, I found that using a while loop ensured that the function would opperate while being under 10. This is the only function that would work for it in my knowledge.

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 item = "burger"

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

What I changed:

  • I changed the code from outputting the even numbers under 10 to outputting the odd numbers under 10.
  • We can do this by defining the odds, starting with 1, and then by increasing with the same increment of 2 to output the later numbers.
  • This will generate a list for the odd numbers.

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)