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:

%%html

<script>	
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];

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

console.log(alphabetList);
</script>	

What I Changed

I changed the amount it went to in the forloop since before it said i<10 while there are 26 letters in the alphabet not 10. I also changed the elements of the array so it adds a letter instead of a number.

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:

%%html
<script>
  var alphabet = "abcdefghijklmnopqrstuvwxyz";
  var alphabetList = [];

  // Corrected loop to push letters from the 'alphabet' string
  for (var i = 0; i < 26; i++) {
    alphabetList.push(alphabet[i]);
  }

  let letter = "e";

  for (var i = 0; i < 26; i++) {
    if (alphabetList[i] == letter) {
      console.log(i+1," is the number of theletter of the alphabet given: ", letter);
      break;  // Exit the loop once we find the letter
    }
  }
</script>


What I Changed

I changed there were 3 equal signs before when comparing both values. I also changed it so it doesn’t take in the number rather the letter instead. I changed the values compared and the input.

Segment 3: Odd Numbers

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

Code:

%%html
<script>
  let odds = [];
  let i1 = 1;

  while (i1 <= 10) {
    odds.push(i1);
    i1 += 2;
  }

  console.log(odds);
</script>

What I Changed

I changed the value for the starting of I since it was starting at 0 and pushing all the even numbers to the array instead of the odd numbers.

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? Some values are printed twice since they are divisble by both 2 and 5. To fix this we can use the else-if condition type for divisbility by 2.
  • Make changes to get the intended outcome.
%%html
<script>
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])
    else if (numbers[i] % 2 === 0)
        newNumbers.push(numbers[i])
}
console.log(newNumbers) 
</script>


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)

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)