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

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

What I Changed

I changed the code from i to alphabet i to iterate the first 10 letters of the alphabet instead of listing out the numbers 1-10. This made it so that when you pushed the string it pushed the letters of the alphabet from for values of i from 1-10 instead of just i being the numbers 1-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 < 10; i++) {
    alphabetList.push(alphabet[i]);
}

let letterNumber = 4;

for (var i = 0; i < alphabetList.length; i++) {
    if (i === letterNumber) {
        console.log('"' + alphabetList[i] + '" is letter number ' + (i + 1) + ' in the alphabet');
    }
}
<IPython.core.display.Javascript object>

What I Changed

I changed the letterNumber value to 4 since lists are counted starting from 0. This made it so that when you output the letter “e” it is the “fourth” character and when listing its placement in the alphabet it is i + 1 with i being 4 so it is the 5th letter.

Segment 3: Odd Numbers

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

Code:

%%js

let evens = [];
let i = 1;

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

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

What I Changed

I changed i to be 1 making the starting number odd thus causing every addition of 2 to display a new odd number.

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 newNumbers = [];
for (var i = 1; i <= 100; i++) {
    if (i % 2 === 0 || i % 5 === 0) {
        newNumbers.push(i);
    }
}
console.log(newNumbers);
<IPython.core.display.Javascript object>

I asked it to add numbers with a quotient of 2 or 5 to the list.

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;
  
  console.log("Menu:");
  for (var item in menu) {
    console.log(item + "  $" + menu[item].toFixed(2));
  }
  
  var selectedItem = "burger";
  
  if (menu[selectedItem]) {
    total = menu[selectedItem];
    console.log(selectedItem + " added to the order.");
  } else {
    console.log(selectedItem + " is not on the menu.");
  }
  
  console.log("Total: $" + total.toFixed(2));  
<IPython.core.display.Javascript object>

What I Changed

In the modified code, I removed the array selectedItems and directly assigned the “burger” as the selected item. Additionally, I added a conditional check to ensure that the selected item exists in the menu object and then calculated the total cost based on the selected item.

ccc

  • 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)
%%html

<script src="https://utteranc.es/client.js"
        repo="ninaadkiran/Ninaad-Repository2"
        issue-term="pathname"
        theme="github-dark-orange"
        crossorigin="anonymous"
        async>
</script>