Skip to content

exceptions exercises and chapter examples #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions exceptions/chapter-examples/finally/finally.js
Original file line number Diff line number Diff line change
@@ -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...");
15 changes: 15 additions & 0 deletions exceptions/chapter-examples/finally/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 1 addition & 0 deletions exceptions/chapter-examples/throw-default-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw Error('You cannot divide by zero!');
15 changes: 15 additions & 0 deletions exceptions/chapter-examples/try-catch/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions exceptions/chapter-examples/try-catch/try-catch.js
Original file line number Diff line number Diff line change
@@ -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...");
7 changes: 7 additions & 0 deletions exceptions/exercises/divide.js
Original file line number Diff line number Diff line change
@@ -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:
24 changes: 24 additions & 0 deletions exceptions/exercises/test-student-labs.js
Original file line number Diff line number Diff line change
@@ -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);