Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Tunzale1 authored Jul 7, 2023
1 parent 35aa8c7 commit f7fcfda
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
Binary file added hw2/assets/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hw2/assets/image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hw2/assets/image-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hw2/assets/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions hw2/index.html
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>
10 changes: 10 additions & 0 deletions hw2/readme.md
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.
- ![Alt text](assets/image.png)
- ![Alt text](assets/image-1.png)
- 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.
- ![Alt text](assets/image-2.png)
- ![Alt text](assets/image-3.png)
56 changes: 56 additions & 0 deletions hw2/script.js
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);

0 comments on commit f7fcfda

Please sign in to comment.