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:
let alphabetList = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
let letterNumber = 5;
for (var i = 0; i < alphabetList.length; i++) { // Use alphabetList.length to specify the array's length
if (i + 1 === letterNumber) { // Check if (i + 1) equals letterNumber to find the position in the alphabet
console.log(alphabetList[i] + " is letter number " + letterNumber + " in the alphabet");
}
}
// Should output:
// "e" is letter number 5 in the alphabet
What I Changed
I changed… the alphabet list from “abcdefghijklmnopqrstuvwxyz” to make it seperated and make the number 5 letter become e instead of all being mashed together.
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
let alphabetList = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
// Specify the letterNumber
let letterNumber = 5;
for (var i = 0; i < alphabetList.length; i++) {
if (i + 1 === letterNumber) {
console.log(alphabetList[i] + " is letter number " + letterNumber + " in the alphabet");
}
}
What I Changed
I changed… the first part and defined alphabetList as letters A-Z. I also Corrected the loop condition to use i < alphabetList.length to apply to the entire alphabet.
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… the let statement to let i=1 so that the list starts at 1 and adds 2 each time, so the list is outputted as odd numbers instead of starting from 0 making it even 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?
- 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)
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)