diff --git a/README.md b/README.md index a3a32bc2..ec9422d0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # learn-js JavaScript tutorial repo -### REPLACE WITH YOUR FULL NAME +### Yuanyuan Yang diff --git a/quiz/exception.html b/quiz/exception.html index c154b213..a01e0d2c 100644 --- a/quiz/exception.html +++ b/quiz/exception.html @@ -24,8 +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 (number < 0) { + throw new Error(`Cannot calculate square root of negative number: ${number}`); + } + return Math.sqrt(number); + } catch (error) { + console.error(error.message); + return NaN; + } + }); // Displaying the original and transformed arrays console.log('Original Numbers Array:', numbers); diff --git a/quiz/scripts/map-deep.js b/quiz/scripts/map-deep.js index e97665e5..8635a723 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 [...row].sort((a, b) => a - b); }); // Modifying the original matrix (changing the last element of the first row) @@ -15,4 +15,4 @@ const originalMatrix = [ // Displaying the original and new matrices console.log('Original Matrix (modified):', originalMatrix); console.log('New Matrix (unaffected):', newMatrix); - \ No newline at end of file + diff --git a/quiz/scripts/stack-q1.js b/quiz/scripts/stack-q1.js index 0d719223..40fd495e 100644 --- a/quiz/scripts/stack-q1.js +++ b/quiz/scripts/stack-q1.js @@ -9,6 +9,14 @@ class PStack { return this.#id; } + get persons() { + return this._persons.slice(); + } + + addPerson(person) { + this._persons.push(person); + } + } class PStackImpl extends PStack { @@ -21,6 +29,10 @@ class PStackImpl extends PStack { } pop() { + if (this.persons.length === 0) { + console.error('No persons to pop.'); + return NaN; + } return this._persons.pop().age } }