From 42bb9d537202d1cca8abc4d870127b3d92c89206 Mon Sep 17 00:00:00 2001 From: Daniel_Chinn Date: Fri, 9 Feb 2024 23:44:10 -0500 Subject: [PATCH] updated various files --- quiz/exception.html | 11 ++++++++++- quiz/scripts/map-deep.js | 2 +- quiz/scripts/stack-q1.js | 8 ++++++++ scripts/exception.js | 2 +- scripts/math.js | 5 ++--- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/quiz/exception.html b/quiz/exception.html index c154b213..c34055e2 100644 --- a/quiz/exception.html +++ b/quiz/exception.html @@ -24,7 +24,16 @@

Error Handling with Array Map

// Using map to calculate the square root of each number // Use Math.sqrt(number) to calculate sq root const squareRoots = numbers.map(number => { - return 0 + try { + if(isNaN(number)) throw `${number} is not a valid number` + if(number < 0) throw `${number} is negative` + return Math.sqrt(number) + } + + catch(err) { + console.log(`Error: ${err}`) + return NaN + } }); // Displaying the original and transformed arrays diff --git a/quiz/scripts/map-deep.js b/quiz/scripts/map-deep.js index e97665e5..d43455c6 100644 --- a/quiz/scripts/map-deep.js +++ b/quiz/scripts/map-deep.js @@ -6,7 +6,7 @@ const originalMatrix = [ ]; const newMatrix = originalMatrix.map((row) => { - return row.sort(); + return Array.from(row).sort(); }); // Modifying the original matrix (changing the last element of the first row) diff --git a/quiz/scripts/stack-q1.js b/quiz/scripts/stack-q1.js index 0d719223..72aa78ea 100644 --- a/quiz/scripts/stack-q1.js +++ b/quiz/scripts/stack-q1.js @@ -16,6 +16,14 @@ class PStackImpl extends PStack { super(); } + get persons() { + return this._persons; + } + + set persons(persons) { + this._persons = persons; + } + push(p) { return this._persons.push(p) } diff --git a/scripts/exception.js b/scripts/exception.js index c5e554fa..98582256 100644 --- a/scripts/exception.js +++ b/scripts/exception.js @@ -6,7 +6,7 @@ window.onload = function() { let x = document.getElementById("demo").value; try { if(x == "") throw 'empty'; - if(isNan(x)) throw 'not a number'; + if(isNaN(x)) throw 'not a number'; x = Number(x); if(x < 5) throw 'too low'; if(x > 10) throw 'too high'; diff --git a/scripts/math.js b/scripts/math.js index 75ad0f71..64d0f6c7 100644 --- a/scripts/math.js +++ b/scripts/math.js @@ -7,6 +7,5 @@ export function add(a, b) { // Function to multiply two numbers export function multiply(a, b) { -return a * b; -} - \ No newline at end of file + return a * b; +} \ No newline at end of file