-
-
Notifications
You must be signed in to change notification settings - Fork 83
NW6 | Nazanin Saedi | object-destructuring | Module-JS3 | Exercise 3 | Week 3 #322
base: main
Are you sure you want to change the base?
Changes from all commits
6133172
0082b5a
85a2bfb
3c58f2f
809a154
3855496
dd7f745
d2bacff
caa6eb7
5385105
8f83c95
10791ff
13ab837
de16492
3d12923
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"liveServer.settings.port": 5501 | ||
} |
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 */ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
const personOne = { | ||
name: "Popeye", | ||
age: 34, | ||
favouriteFood: "Spinach", | ||
}; | ||
// const personOne = { | ||
// name: "Popeye", | ||
// age: 34, | ||
// favouriteFood: "Spinach", | ||
// }; | ||
|
||
function introduceYourself(___________________________) { | ||
// function introduceYourself(___________________________) { | ||
// console.log( | ||
// `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` | ||
// ); | ||
// } | ||
|
||
// introduceYourself(personOne); | ||
function introduceYourself({ name, age, favouriteFood }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, avoid having commented out code in your PRs. I would just delete the block, but if I know that I might need it in the future, I would write down previous commit hash for the reference. |
||
console.log( | ||
`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` | ||
); | ||
} | ||
|
||
introduceYourself(personOne); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,19 @@ let hogwarts = [ | |
occupation: "Teacher", | ||
}, | ||
]; | ||
|
||
// Task 1: | ||
console.log("Task 1:"); | ||
let GryffindorMembers = hogwarts.filter(({ house }) => house === "Gryffindor"); | ||
GryffindorMembers.forEach(({ firstName, lastName }) => { | ||
console.log('${firstName} ${lastName}'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, us back ticks (`) here like this: console.log(`${firstName} ${lastName}`); |
||
|
||
//Task 2 | ||
console.log("Task 2:"); | ||
let teachersWithPets = hogwaters.filter(({ occupation, pet }) => occupation === "Teacher" && pet !== null); | ||
teachersWithPets.forEach(({ firstName, lastName }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is just a matter of coding style, but I prefer to put constants to the left in the comparisons, like this:
The reason is that if you will try to make a silly mistake like this: "Teacher" = occupation JS will not compile and throw an error. Because you can't assign a new value to a constant. While in case of occupation = "Teacher" JS will eagerly accept it as valid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you want to check for instead of:
use:
|
||
console.log(`${firstName} ${lastName}`); | ||
}); | ||
|
||
|
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> |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need await in front of response.json() here. You already waited for the fetch(). |
||
|
||
// 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; |
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nazanin, I would suggest instead of explicitly checking for null/empty string here, use falsy check like this:
if (!title.value || ! author.value || !pages.value) { ... }
This way JavaScript will check for null/undefined/empty string for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to check for some value, please, consider using === instead of ==. This way JS will check for the value and for the type.