-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Closure</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="./script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*Theoretical Question* | ||
- Provide a few examples of when it is appropriate to use the try...catch construct in code. | ||
- The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed. | ||
- Mainly i use try...catch to handle an error. | ||
-  | ||
-  | ||
- Parsing and validating input: | ||
- When parsing or validating user input, there might be cases where the input doesn't meet the expected format or requirements. In such problems, you can use try...catch to catch and handle those errors. For example, when parsing JSON data, you can use try...catch to handle potential parsing errors. | ||
-  | ||
-  |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const books = [ | ||
{ | ||
author: "Lucy Foley", | ||
name: "List of Invitees", | ||
price: 70 | ||
}, | ||
{ | ||
author: "Susanna Clarke", | ||
name: "Jonathan Strange & Mr Norrell", | ||
}, | ||
{ | ||
name: "Design. A Book for Non-Designers.", | ||
price: 70 | ||
}, | ||
{ | ||
author: "Alan Moore", | ||
name: "Neonomicon", | ||
price: 70 | ||
}, | ||
{ | ||
author: "Terry Pratchett", | ||
name: "Moving Pictures", | ||
price: 40 | ||
}, | ||
{ | ||
author: "Angus Hyland", | ||
name: "Cats in Art", | ||
} | ||
]; | ||
|
||
const root = document.getElementById("root"); | ||
const ul = document.createElement("ul"); | ||
|
||
function createBookList(books) { | ||
books.forEach(book => { | ||
if (validateBook(book)) { | ||
const li = document.createElement("li"); | ||
const bookInfo = Object.entries(book).map(([key, value]) => `${key} - ${value}`).join(" / "); | ||
li.textContent = bookInfo; | ||
ul.appendChild(li); | ||
} | ||
}); | ||
|
||
root.appendChild(ul); | ||
} | ||
|
||
function validateBook(book) { | ||
const requiredFields = ['author', 'name', 'price']; | ||
const missingFields = requiredFields.filter(field => !Object.keys(book).includes(field)); | ||
if (missingFields.length > 0) { | ||
console.error("Missing: ", missingFields, book); | ||
return false; | ||
} | ||
return true; | ||
} | ||
createBookList(books); |