Skip to content

Latest commit

 

History

History
378 lines (315 loc) · 6.41 KB

File metadata and controls

378 lines (315 loc) · 6.41 KB

Errors

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

  1. Paste the broken code into the devtools console and run it
  2. Copy-paste the error message into the "error message" space the challenge
  3. Classify the error: Creation or Execution and Syntax or Semantic
  4. 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.

Index:



Learning Objectives

  • 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

Exercises

malformed while loop

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:

TOP


missing variable name

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:

TOP


too-far object access

broken code:

let a = {b:3};
let b = a.b.3

error message:

SyntaxError: Unexpected number

classification:

  • creation phase or execution phase ?
  • syntax or semanitc ?

the fix:

let a = {b:3};
let b = a.b;

your notes:

TOP


access property directly

broken code:

let x = {b:'e'};
let y = b.e;

error message:

pythontutor gives no mistake

classification:

  • creation phase or execution phase ?
  • syntax or semanitc ?

the fix:

let x = {b:'e'};
let y = x;

your notes:

TOP


improper multi-line string

broken code:

let a = 'this is 
two lines';

error message:

SyntaxError: Unexpected token ILLEGAL

classification:

  • creation phase or execution phase ?
  • syntax or semanitc ?

the fix:

let a = 'this is\n two lines';

your notes:

TOP


improper end of statement

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:

TOP


malformed array

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:

TOP


missing arguments

broken code:

function getNine {
  let x = 6;
  let y = 3;
  return x + y;
}
let result = getNine();

error message:

SyntaxError: Unexpected token

classification:

  • 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:

TOP


improper nested quotes 1

broken code:

let innerHtml = "<p>Click here to <a href="#Home">return home</a></p>";

error message:

SyntaxError: Unexpected token ILLEGAL

classification:

  • 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:

TOP


improper nested quotes 2

broken code:

let nested_messages = 'remind yourself ''i can do this!'' at least once a day';

error message:

SyntaxError: Unexpected string

classification:

  • 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:

TOP


reassigning to constant

broken code:

const a = 9;
a = 0;

error message:

TypeError: Assignment to constant variable

classification:

  • creation phase or execution phase ?
  • syntax or semanitc ?

the fix:

let a = 9;
a = 0;

your notes: const value cannot b reassigned TOP


unassigned const declaration

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:

TOP


is not a function

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:

TOP