Home | HTML | Data Types | DOM | JavaScript | JS Debugging |
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
I changed line 7 code originally from alphabetList.push(i) to alphabetList.push(alphabet[i]). The original code pushed the value of variable i into the alphabetList array, which resulted in an array of numbers from 0 to 9. In the modified code, I changed it to alphabet[i] to push the characters from the alphabet string at the corresponding index i into the alphabetList array.
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 letterNumber = 5;
var alphabet = "abcdefghijklmnopqrstuvwxyz";
if (letterNumber >= 1 && letterNumber <= alphabet.length) {
let letter = alphabet[letterNumber - 1];
console.log(letter + " is letter number " + letterNumber + " in the alphabet");
} else {
console.log("Invalid letter number. It should be between 1 and 26.");
}
<IPython.core.display.Javascript object>
What I Changed
I added the alphabet variable to store the alphabet string. I also added a condition to check if letterNumber is within the valid range (1 through 26). The code calculates the letter by accessing the corresponding character in the alphabet string using letterNumber - 1. The -1 is important because of JavaScript’s 0-based indexing.
Segment 3: Odd Numbers
Intended behavior: print a list of all the odd numbers below 10
Code:
%%js
let odds = [];
let i = 1; // Start with the first odd number
while (i < 10) { // Check if the number is less than 10
odds.push(i);
i += 2; // Increment by 2 to get the next odd number
}
console.log(odds);
<IPython.core.display.Javascript object>
What I Changed
I changed the variable name from evens to odds as the new code finds the odd numbers under 10 instead of the even numbers. I initialized i with 1 to start with the first odd number under 10, which is 1.
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 || numbers[i] % 2 === 0)
newNumbers.push(numbers[i])
}
console.log(newNumbers)
<IPython.core.display.Javascript object>
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)