diff --git a/exceptions/chapter-examples/finally/finally.js b/exceptions/chapter-examples/finally/finally.js new file mode 100644 index 0000000000..33c9fe2272 --- /dev/null +++ b/exceptions/chapter-examples/finally/finally.js @@ -0,0 +1,14 @@ +const input = require('readline-sync'); + +let animals = [{name: 'cat'}, {name: 'dog'}]; +let index = Number(input.question("Enter index of animal:")); + +try { + console.log('animal at index:', animals[index].name); +} catch(TypeError) { + console.log("We caught a TypeError, but our program continues to run!"); +} finally { + console.log("You tried to access an animal at index:", index); +} + +console.log("the code goes on..."); diff --git a/exceptions/chapter-examples/finally/package.json b/exceptions/chapter-examples/finally/package.json new file mode 100644 index 0000000000..c447dfacce --- /dev/null +++ b/exceptions/chapter-examples/finally/package.json @@ -0,0 +1,15 @@ +{ + "name": "control-flow-type-error-finally", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "readline-sync": "^1.4.10" + } +} diff --git a/exceptions/chapter-examples/throw-default-error.js b/exceptions/chapter-examples/throw-default-error.js new file mode 100644 index 0000000000..7c01c6b9ad --- /dev/null +++ b/exceptions/chapter-examples/throw-default-error.js @@ -0,0 +1 @@ +throw Error('You cannot divide by zero!'); diff --git a/exceptions/chapter-examples/try-catch/package.json b/exceptions/chapter-examples/try-catch/package.json new file mode 100644 index 0000000000..d805cc4cc0 --- /dev/null +++ b/exceptions/chapter-examples/try-catch/package.json @@ -0,0 +1,15 @@ +{ + "name": "control-flow-type-error", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "readline-sync": "^1.4.10" + } +} diff --git a/exceptions/chapter-examples/try-catch/try-catch.js b/exceptions/chapter-examples/try-catch/try-catch.js new file mode 100644 index 0000000000..15b0192f85 --- /dev/null +++ b/exceptions/chapter-examples/try-catch/try-catch.js @@ -0,0 +1,13 @@ +const input = require('readline-sync'); + +let animals = [{name: 'cat'}, {name: 'dog'}]; +let index = Number(input.question("Enter index of animal:")); + +try { + console.log('animal at index:', animals[index].name); +} catch(TypeError) { + console.log("We caught a TypeError, but our program continues to run!"); + console.log("You tried to access an animal at index:", index); +} + +console.log("the code goes on..."); diff --git a/exceptions/exercises/divide.js b/exceptions/exercises/divide.js new file mode 100644 index 0000000000..06fc889862 --- /dev/null +++ b/exceptions/exercises/divide.js @@ -0,0 +1,7 @@ +// Write a function called 'divide' that takes two parameters: a numerator and a denominator. + +// Your function should return the result of numerator / denominator. + +// However, if the denominator is zero you should throw the error, "Attempted to divide by zero." + +// Code your divide function here: diff --git a/exceptions/exercises/test-student-labs.js b/exceptions/exercises/test-student-labs.js new file mode 100644 index 0000000000..cfe5bfe175 --- /dev/null +++ b/exceptions/exercises/test-student-labs.js @@ -0,0 +1,24 @@ +function gradeLabs(labs) { + for (let i=0; i < labs.length; i++) { + let lab = labs[i]; + let result = lab.runLab(3); + console.log(`${lab.student} code worked: ${result === 27}`); + } +} + +let studentLabs = [ + { + student: 'Carly', + runLab: function (num) { + return Math.pow(num, num); + } + }, + { + student: 'Erica', + runLab: function (num) { + return num * num; + } + } +]; + +gradeLabs(studentLabs);