1.4 Correcting errors
Practice with identifying and correcting code blocks
Home | HTML | Data Types | DOM | JavaScript | JS Debugging |
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)
Segment 1: Alphabet List
Intended behavior: create a list of characters from the string contained in the variable alphabet
Code:
What I Changed
I changed alphabetList.push(i);
to alphabetList.push(alphabet[i])
so that the console would print characters from the variable alphabet and changed 10
to alphabet.length
to print every character in the variable.
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:
What I Changed
I changed for (var i = 0; i < alphabetList; i++)
to for (var i = 0; i < alphabetList.length; i++)
to make sure that i was changing with regards to the length of alphabetList
. Then I changed console.log(letterNumber)
to console.log(alphabet[letterNumber])
so that the number 5 would correspond to the numbered character in the variable alphabet. After that I changed it to console.log(alphabet[letterNumber - 1]
so it would return e and not f, since computers start counting from 0.
Segment 3: Odd Numbers
Intended behavior: print a list of all the odd numbers below 10
Code:
What I Changed
I changed let i = 0
to let i = 1
so that for every while loop, 2 is added to 1 instead of 0 so that the function prints odd numbers instead of even.
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.
What I Changed
I changed the for loop to include both conditions in the same parameter, then wrote code to only push the number if not already included in the array.
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.