Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# learn-js
JavaScript tutorial repo

### REPLACE WITH YOUR FULL NAME
### Yuanyuan Yang
12 changes: 10 additions & 2 deletions quiz/exception.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ <h1>Error Handling with Array Map</h1>
// 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);
Expand Down
4 changes: 2 additions & 2 deletions quiz/scripts/map-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -15,4 +15,4 @@ const originalMatrix = [
// Displaying the original and new matrices
console.log('Original Matrix (modified):', originalMatrix);
console.log('New Matrix (unaffected):', newMatrix);


12 changes: 12 additions & 0 deletions quiz/scripts/stack-q1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down