-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b93605
commit ea074b9
Showing
30 changed files
with
886 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
/* console.error | ||
another useful console message is console.error | ||
this console methods makes things show up just like when an error occurs | ||
using console.assert will also appear with (x) in the debugger | ||
it's conventionally used along with try/catch | ||
*/ | ||
|
||
console.log('hello!'); | ||
|
||
console.error('hello!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
/* try/catch blocks | ||
errors that happen in execution phase can can be handled using try/catch | ||
normally when an error occurs, your program stops | ||
with try/catch you can decide what happens when an error occurs | ||
this doesn't mean you can't be careful :) | ||
you won't need to use try/catch very often at all during HYF | ||
it's just nice to know about, and helpful for understanding errors | ||
*/ | ||
|
||
const notAFunction = 'not a function'; | ||
|
||
try { | ||
// trying to call the string will throw an error | ||
notAFunction(); | ||
// nothing in the try block will be executed after the error | ||
console.log('never happens, error!'); | ||
} catch (err) { | ||
// the error is passed as an argument to the catch block | ||
// you can do whatever you like in the catch block | ||
console.log(err.name); | ||
console.log(err.message); | ||
} | ||
|
||
try { | ||
notAFunction(); | ||
} catch (potato) { | ||
// you can use any variable name in the parenthesis | ||
console.error(potato); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
/* new Error() | ||
errors aren't magic, you can create your own! | ||
there are a few different types of errors | ||
let's look at some of the errors you may have come across in your code | ||
*/ | ||
|
||
const newError1 = new Error('hello world!'); | ||
console.log(newError1.name); | ||
console.log(newError1.message); | ||
console.error(newError1); | ||
|
||
const newTypeError1 = new TypeError('something is not a something'); | ||
console.log(newTypeError1.name); | ||
console.log(newTypeError1.message); | ||
console.error(newTypeError1); | ||
|
||
const newRangeError1 = new RangeError('loopGuard_5 exceeded 15 iterations'); | ||
console.log(newRangeError1.name); | ||
console.log(newRangeError1.message); | ||
console.error(newRangeError1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
/* throw new Error() | ||
before you saw how to create new errors, but that's still not the whole story | ||
what if your errors could stop the program just like native errors? | ||
you can! | ||
*/ | ||
|
||
try { | ||
throw new Error('magic'); | ||
console.log('never happens!'); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
|
||
// you can throw anything you want, it's just not common practice | ||
try { | ||
throw 4; | ||
} catch (number) { | ||
console.error(number); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
/* Replicate Errors | ||
each exercise in this chapter will have two parts | ||
1. cause the asserted error by writing broken JavaScript | ||
2. throw the same error manually | ||
you will want to use Chrome/ium for these exercises | ||
the assertions were written for v8 error messages | ||
*/ | ||
|
||
console.log('- broken JS -'); | ||
try { | ||
const x = true; | ||
x = false; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
|
||
console.log('- throw new -'); | ||
try { | ||
throw new TypeError('Assignment to constant variable.'); | ||
} catch (err) { | ||
console.error(err); | ||
|
||
console.assert(err.name === 'TypeError', 'name fail'); | ||
console.assert( | ||
err.message === 'Assignment to constant variable.', | ||
'message fail', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
// this assertion checks to make sure a function throws an error | ||
// .toThrow is a challenge, not required for class | ||
// you can skip this example if you want to | ||
describe('.toThrow', () => { | ||
it('should throw any error', () => { | ||
const throwsAnError = () => { | ||
null.someProperty; | ||
}; | ||
expect(throwsAnError).toThrow(); | ||
}); | ||
it('should throw a specific message', () => { | ||
const shouldThrow = () => { | ||
null(); | ||
}; | ||
expect(shouldThrow).toThrow('null is not a function'); | ||
}); | ||
it('should throw a specific error type', () => { | ||
const shouldThrow = () => { | ||
null(); | ||
}; | ||
expect(shouldThrow).toThrow(TypeError); | ||
}); | ||
it("being more specific doesn't make a difference", () => { | ||
const shouldThrow = () => { | ||
null(); | ||
}; | ||
expect(shouldThrow).toThrow(new TypeError('null is not a function')); | ||
}); | ||
it('failing: does not throw', () => { | ||
const doesNotThrow = () => { | ||
'hello!'; | ||
}; | ||
expect(doesNotThrow).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
/* | ||
"guards" is when a function throws an error if arguments are not correct in some way | ||
this way your functions will not silently do the wrong thing | ||
and when you code stops on an error, you can figure out where the mistake comes from | ||
There are no exercises for testing type guards in this folder | ||
you can practice this in /function-design and /fuzz-testing | ||
*/ | ||
|
||
/** | ||
* Adds two numbers together. | ||
* If either parameter is not a number or is NaN, the function throws a TypeError | ||
* | ||
* @param {number} [a=0] - the left operand for addition | ||
* @param {number} [b=0] - the right operand for addition | ||
* @returns {number} the sum of a and b if they are numbers and not NaN | ||
* | ||
* @throws {TypeError} a is not a number | ||
* @throws {TypeError} a is NaN | ||
* @throws {TypeError} b is not a number | ||
* @throws {TypeError} b is NaN | ||
*/ | ||
const addTwoNumbers = (a = 0, b = 0) => { | ||
if (typeof a !== 'number') { | ||
throw new TypeError('a is not a number'); | ||
} | ||
if (Number.isNaN(a)) { | ||
throw new TypeError('a is NaN'); | ||
} | ||
if (typeof b !== 'number') { | ||
throw new TypeError('b is not a number'); | ||
} | ||
if (Number.isNaN(b)) { | ||
throw new TypeError('b is NaN'); | ||
} | ||
|
||
return a + b; | ||
}; | ||
|
||
describe('addTwoNumbers will only add two numbers', () => { | ||
describe('it adds two numbers as expected', () => { | ||
it('adds positive numbers', () => { | ||
expect(addTwoNumbers(1, 3)).toEqual(4); | ||
}); | ||
it('adds negative numbers', () => { | ||
expect(addTwoNumbers(-1, -4)).toEqual(-5); | ||
}); | ||
it('adds mixed +/- numbers', () => { | ||
expect(addTwoNumbers(1, -2)).toEqual(-1); | ||
}); | ||
it('adds decimal numbers numbers', () => { | ||
expect(addTwoNumbers(1.5, 1.5)).toEqual(3); | ||
}); | ||
}); | ||
describe('default parameters are 0', () => { | ||
it('second parameter defaults to 0', () => { | ||
expect(addTwoNumbers(1)).toEqual(1); | ||
}); | ||
it('both parameters default to 0', () => { | ||
expect(addTwoNumbers()).toEqual(0); | ||
}); | ||
}); | ||
describe('both parameters have a type guard', () => { | ||
it('a must be a number', () => { | ||
const shouldThrow = () => { | ||
addTwoNumbers('2', 1); | ||
}; | ||
expect(shouldThrow).toThrow(new TypeError('a is not a number')); | ||
}); | ||
it('a cannot be NaN', () => { | ||
const shouldThrow = () => { | ||
addTwoNumbers(NaN, 1); | ||
}; | ||
expect(shouldThrow).toThrow(new TypeError('a is NaN')); | ||
}); | ||
it('b must be a number', () => { | ||
const shouldThrow = () => { | ||
addTwoNumbers(1, true); | ||
}; | ||
expect(shouldThrow).toThrow(new TypeError('b is not a number')); | ||
}); | ||
it('b cannot be NaN', () => { | ||
const shouldThrow = () => { | ||
addTwoNumbers(1, NaN); | ||
}; | ||
expect(shouldThrow).toThrow(new TypeError('b is NaN')); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
console.log('- broken JS -'); | ||
try { | ||
null(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
|
||
console.log('- throw new error -'); | ||
try { | ||
throw __; | ||
} catch (err) { | ||
console.error(err); | ||
|
||
console.assert(err.name === 'TypeError', 'name fail'); | ||
console.assert(err.message === 'null is not a function', 'message fail'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
console.log('- broken JS -'); | ||
try { | ||
let name; | ||
name.length; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
|
||
console.log('- throw new error -'); | ||
try { | ||
throw __; | ||
} catch (err) { | ||
console.error(err); | ||
|
||
console.assert(err.name === 'TypeError', 'name fail'); | ||
console.assert( | ||
err.message === "Cannot read property 'length' of undefined", | ||
'message fail', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
console.log('- broken JS -'); | ||
try { | ||
const stringVariable = 'hello!'; | ||
stringVariable(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
|
||
console.log('- throw new error -'); | ||
try { | ||
throw __; | ||
} catch (err) { | ||
console.error(err); | ||
|
||
console.assert(err.name === 'TypeError', 'name fail'); | ||
console.assert( | ||
err.message === 'stringVariable is not a function', | ||
'message fail', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// #todo | ||
|
||
'use strict'; | ||
|
||
console.log('- broken JS -'); | ||
try { | ||
turtle = 4; | ||
let turtle; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
|
||
console.log('- throw new error -'); | ||
try { | ||
throw __; | ||
} catch (err) { | ||
console.error(err); | ||
|
||
console.assert(err.name === 'ReferenceError', 'name fail'); | ||
console.assert( | ||
err.message === "Cannot access 'turtle' before initialization", | ||
'message fail', | ||
); | ||
} |
Oops, something went wrong.