Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NW6 | Nazanin Saedi | JS3 | code-reading | Module-JS3 | week3 #318

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
26 changes: 12 additions & 14 deletions book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title> </title>
<title>My Book Library</title> <!-- Added a title for your webpage -->
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<!-- Updated script sources to use HTTPS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<!-- Updated CSS source to use HTTPS -->
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
<!-- Included your custom CSS file -->
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

Expand All @@ -31,15 +34,15 @@ <h1>Library</h1>
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text" <!-- Corrected input type to "text" -->
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>
<input
type="author"
type="text" <!-- Corrected input type to "text" -->
class="form-control"
id="author"
name="author"
Expand All @@ -61,12 +64,13 @@ <h1>Library</h1>
value=""
/>Read
</label>
<input
type="submit"
value="Submit"
<button
type="button" <!-- Changed input type to button -->
class="btn btn-primary"
onclick="submit();"
/>
>
Submit
</button>
</div>
</div>

Expand All @@ -81,13 +85,7 @@ <h1>Library</h1>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<!-- Removed the default table row -->
</tbody>
</table>

Expand Down
37 changes: 16 additions & 21 deletions book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ window.addEventListener("load", function (e) {

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); // Corrected book title
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
Expand All @@ -25,20 +25,20 @@ const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");

//check the right input from forms and if its ok -> add the new book (object in array)
//via Book function and start render function
function submit() {
if (
title.value == null ||
title.value == "" ||
author.value == null || // Check for author input
author.value == "" ||
pages.value == null ||
pages.value == ""
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
let book = new Book(title.value, author.value, pages.value, check.checked); // Passed author.value instead of title.value
myLibrary.push(book);
render();
}
}
Expand All @@ -53,11 +53,11 @@ function Book(title, author, pages, check) {
function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
// Delete old table rows
for (let n = rowsNumber - 1; n > 0; n--) { // Corrected the loop condition
table.deleteRow(n);
}
//insert updated row and cells
// Insert updated rows and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
Expand All @@ -70,31 +70,26 @@ function render() {
cell2.innerHTML = myLibrary[i].author;
cell3.innerHTML = myLibrary[i].pages;

//add and wait for action for read/unread button
// Add read/unread button
let changeBut = document.createElement("button");
changeBut.id = i;
changeBut.className = "btn btn-success";
cell4.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
let readStatus = myLibrary[i].check ? "No" : "Yes"; // Corrected readStatus assignment
changeBut.innerHTML = readStatus;

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});

//add delete button to every row and render again
// Add delete button
let delButton = document.createElement("button");
delBut.id = i + 5;
cell5.appendChild(delBut);
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {
delButton.id = i + 5; // Corrected variable name from delBut to delButton
cell5.appendChild(delButton);
delButton.className = "btn btn-warning";
delButton.innerHTML = "Delete";
delButton.addEventListener("click", function () { // Corrected event name from "clicks" to "click"
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
Expand Down
30 changes: 22 additions & 8 deletions book-library/style.css
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
/* Adjusted styles for the form group */
.form-group {
width: 400px;
height: 300px;
align-self: left;
padding-left: 20px;
max-width: 400px;
/* Changed width to max-width for responsiveness */
margin: 0 auto;
/* Center the form group */
padding: 20px;
}

/* Adjusted styles for buttons */
.btn {
display: block;
display: inline-block;
/* Changed display to inline-block for buttons to appear inline */
margin-right: 10px;
/* Added margin-right for spacing between buttons */
}

/* Adjusted styles for form-check-label */
.form-check-label {
display: inline-block;
/* Changed display to inline-block for checkbox label */
padding-left: 20px;
margin: 5px 0px 5px 0px;
margin: 5px 20px 5px 0;
/* Adjusted margin for spacing */
}

/* Adjusted styles for the add new book button */
button.btn-info {
margin: 20px;
}
margin: 20px auto;
/* Center the button */
display: block;
/* Added display: block for full-width button */
}
20 changes: 20 additions & 0 deletions code-reading/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Take a look at the following code:

Explain why line 4 and line 6 output different numbers.

My answer:
Line 4 creates a new local variable x inside the function f1, which shadows the outer variable x. So, when console.log(x) executes on line 5, it refers to the local x with a value of 2. Line 7 refers to the outer variable x, which retains its original value of 1.
## Question 2

Take a look at the following code:
Expand All @@ -33,6 +35,14 @@ console.log(y);
```

What will be the output of this code. Explain your answer in 50 words or less.
The code output:

10
undefined

# Explain for my answer :
console.log(x) inside f1() prints the value of the outer variable x, which is 10. y is defined locally within f1(), so it's not accessible outside the function, resulting in undefined.


## Question 3

Expand Down Expand Up @@ -60,4 +70,14 @@ f2(y);
console.log(y);
```

answer Q3
THE OUTPUT :

9
{ x: 10 }

MY EXPLAIN
In the first case, x is a primitive (number), so passing it to f1() doesn't change its value. In the second case, y is an object, and since objects are passed by reference, modifying val.x inside f2() affects the original object y.


What will be the output of this code. Explain your answer in 50 words or less.
13 changes: 13 additions & 0 deletions programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>xkcd Comic Viewer</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h1>xkcd Comic Viewer</h1>
<div id="comicContainer"></div>
<script src="script.js"></script>
</body>
26 changes: 26 additions & 0 deletions programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
async function fetchComic(){
try {
const response = await fetch('https://xkcd.now.sh/?comic=latest');
const data = await response.json();

// Log the received data to the console
console.log(data);

//render the comic img
renderComic(data.img);
} catch (error) {
//handle errors
console.error('Error fetching comic:', error);
}
}

//function to render the comic image into the Dom
function renderComic(imgUrl) {
const comicContainer = document.getElementById('comicContainer');
const imgElement = document.createElement('img');
imgElement.src = imgUrl;
imgElement.alt = 'xkcd Comic';
imgElement.id = 'comic';
comicContainer.appendChild(imgElement);
}
window.onload = fetchComic;
5 changes: 5 additions & 0 deletions programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* basic style for the img */
#comic {
max-width: 100%;
height: auto;
}