Errors will keep JS from executing your code. This is not the goal of programming, so here's some errors to practice fixing.
Complete these exercises directly in the browser from your fork of this repo. (We've provided some completed examples for you to get the idea):
- Paste the broken code into the devtools console and run it
- Copy-paste the error message into the "error message" space the challenge
- Classify the error: Creation or Execution and Syntax or Semantic
- Fix the error and paste your fixed code into "the fix" section of the challenge
Practice using the devtools by clicking on the 'VM##:#' link to the right of the error message. Devtools will automatically open the source code and highlight where the error appears. With these super simple examples this feature may feel like overkill, but you will appreciate it's help once you move on to the next set of exercises.
- learning objectives
- exercises
- malfomed while loop (completed example)
- missing variable name
- too-far object access
- access property direclty
- improper multi-line string
- improper end of statement
- malformed array
- missing arguments
- improper nested quotes 1
- improper nested quotes 2
- reassigning to constant
- unassigned const declaration
- is not a function
- study tools
- resources
- reading & identifying error messages
- finding and fixing syntax errors
- building a personal error+fix reference
- not logic errors, that's for 'debugging'
- using devtools console
- writing markdown directly from github
broken code:
let value = 0;
while (value < 9)
value++;
};error message:
Uncaught SyntaxError: Unexpected token }
classification:
- creation phase
- syntax
the fix:
let value = 0;
while (value < 9) {
value++;
};your notes:
broken code:
var = 5;error message:
SyntaxError: Unexpected token classification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
var name= 5;your notes:
broken code:
let a = {b:3};
let b = a.b.3error message:
SyntaxError: Unexpected numberclassification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let a = {b:3};
let b = a.b;your notes:
broken code:
let x = {b:'e'};
let y = b.e;error message:
pythontutor gives no mistakeclassification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let x = {b:'e'};
let y = x;your notes:
broken code:
let a = 'this is
two lines';error message:
SyntaxError: Unexpected token ILLEGALclassification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let a = 'this is\n two lines';your notes:
broken code:
let a = 1:error message:
SyntaxError: Unexpected token classification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let a = 1;your notes:
broken code:
let myArray = [1, 2, 3;error message:
SyntaxError: Unexpected token classification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let myArray = [1, 2, 3];your notes:
broken code:
function getNine {
let x = 6;
let y = 3;
return x + y;
}
let result = getNine();error message:
SyntaxError: Unexpected tokenclassification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
function getNine() {
2 let x = 6;
3 let y = 3;
4 return x + y;
5 }
6 let result = getNine();your notes:
broken code:
let innerHtml = "<p>Click here to <a href="#Home">return home</a></p>";error message:
SyntaxError: Unexpected token ILLEGALclassification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let innerHtml = "<p>Click here to <a href=#Home>return home</a></p>";your notes:
broken code:
let nested_messages = 'remind yourself ''i can do this!'' at least once a day';error message:
SyntaxError: Unexpected stringclassification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let nested_messages = 'remind yourself "i can do this!" at least once a day';your notes:
broken code:
const a = 9;
a = 0;error message:
TypeError: Assignment to constant variableclassification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let a = 9;
a = 0;your notes: const value cannot b reassigned TOP
broken code:
const a;
a = 0;error message:
SyntaxError: Missing initializer in const declaration
classification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
const a = 0;your notes:
broken code:
let array = [];
array.length()error message:
TypeError: array.length is not a function
classification:
- creation phase or execution phase ?
- syntax or semanitc ?
the fix:
let array = [];
array.length;your notes: