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

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 it so now there is one array, and it pushes alphabets index rather than the index as 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:

%%js

var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];

for (var i = 0; i < 10; 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 ${i} in the alphabet`)
	}
}

What I Changed

I changed the for loop now to iterate through the alphabet array’s length and to reference the letters index value rather than the index itself and used a ${} to embed the index without having to use a + operator

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);

What I Changed

I changed the script to start at 1 instead of 0.

BELOW NOT EDITED

The intended outcome is printing a number between 1 and 100 once, if it is a multiple of 2 or 5

%%js

var numbers = []
var i = 0

while (i < 101) {
        if(i % 5 === 0  | i % 2 === 0)
        {
         numbers.push(i)   
        }
       
    i += 1
}
console.log(numbers) 




What I changed

I changed the code to now work without a for loop, the while loop to iterate until 101, and combined the if statments into one

Challenge

This code segment is at a very early stage of implementation.

Hint:

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)) //toFixed makes it only have 2 decimal places
}
//ideally the code should support mutliple items
var order = ["burger","drink","fries"]


//code should add the price of the menu items selected by the user 
for(let i=0;i<order.length;i++)
  {
    let price = menu[order[i]]
    let decimalPlace = (price + "").split(".")[1];
    if(parseInt(decimalPlace) == 99) //4.99 = 5
    {
      price = Math.round(price)
    }
    total += price
  }
console.log(total.toFixed(2))
//

What I Changed

I explained that to fixed is for decimal places, I then made order an array to support multiple choices Then finally I added an additional line to round any prices that were ending in .99 to the next dollar

Hacks