Homework for first week js2 - #504
Conversation
| <body> | ||
| <h1>My book list</h1> | ||
|
|
||
| <script> |
There was a problem hiding this comment.
In practice it is rare to have script inside of a script tag. Try to keep JavaScript external by moving the code to . a separate js file and using src attribute in script element to reference it for example
<script src='./1-books.js' type='text/javascript'></script>The separate code makes it easier for web browsers to cache which enhances performance (here is an article about javascript performance optimisation https://www.keycdn.com/blog/javascript-performance)
| title : 'Underworld', | ||
| author : 'Don DeLillo', | ||
| alreadyRead :true | ||
| } |
There was a problem hiding this comment.
excercise requires three favourite books I see only two 😛
| // 5 | ||
|
|
||
| if (obj.alreadyRead === true) { | ||
| p.style.color = 'green'; |
There was a problem hiding this comment.
instead of using an inline style you can have a css file with class red/green (for example) and toggle classes here is an article that explains more about this:
https://javascript.info/styles-and-classes
| for (let obj of books) { | ||
| // 2 | ||
| const p = document.createElement('p'); | ||
| p.innerText = `${obj.title} was written by ${obj.author.toUpperCase()}`; |
| } | ||
| // 4 | ||
| let img1 = document.createElement('img'); | ||
| let img2 = document.createElement('img'); |
There was a problem hiding this comment.
why did you have img outside the for of loop. Think about how is img1 different than img2 and how can you solve that. In the end you need an image for each book right?
| let img2 = document.createElement('img'); | ||
| img1.src = 'https://upload.wikimedia.org/wikipedia/en/thumb/e/e1/ArtistOfTheFloatingWorld.jpg/220px-ArtistOfTheFloatingWorld.jpg'; | ||
| img2.src = 'http://xoxoafterdark.com/wp-content/uploads/2014/10/Delillo-Underworld.jpg'; | ||
| let lis = document.querySelectorAll('li'); |
There was a problem hiding this comment.
Think about why would you query the list items you already have them inside of the for of loop right?
| img1.src = 'https://upload.wikimedia.org/wikipedia/en/thumb/e/e1/ArtistOfTheFloatingWorld.jpg/220px-ArtistOfTheFloatingWorld.jpg'; | ||
| img2.src = 'http://xoxoafterdark.com/wp-content/uploads/2014/10/Delillo-Underworld.jpg'; | ||
| let lis = document.querySelectorAll('li'); | ||
| lis[0].appendChild(img1).style.width = '350px'; |
| <li>Favorite food: <span id="fav-food"></span></li> | ||
| <li>Hometown: <span id="hometown"></span></li> | ||
| </ul> | ||
| <script> |
There was a problem hiding this comment.
same comment as exercise one javascript and style better be in external files (unless exercise indicates to do so) 👍
| document.querySelector('#hometown').innerText='London'; | ||
|
|
||
| const items=document.querySelectorAll('ul li'); | ||
| items.forEach(item=> {item.classList.add('list-item')}) |
There was a problem hiding this comment.
Good work. However good to keep in mind that this way of iterating is not supported by all browsers Here is more read about iteration options on nodeList
https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/
|
|
||
| const styleTag=document.createElement('style'); | ||
| document.querySelector("head").appendChild(styleTag); | ||
| document.querySelector('style').innerText= '.list-item {color:red}' |
|
|
||
| const img = document.createElement('img'); | ||
| img.src = 'https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/d9585883-0c30-448b-8cee-6eac6d5c695f/d80kp71-a8d13ec7-6e9a-4268-9312-222184443314.png/v1/fill/w_1024,h_576,q_75,strp/real_madrid_david_beckham_wallpaper_by_sameerhd-d80kp71.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwic3ViIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsImF1ZCI6WyJ1cm46c2VydmljZTppbWFnZS5vcGVyYXRpb25zIl0sIm9iaiI6W1t7InBhdGgiOiIvZi9kOTU4NTg4My0wYzMwLTQ0OGItOGNlZS02ZWFjNmQ1YzY5NWYvZDgwa3A3MS1hOGQxM2VjNy02ZTlhLTQyNjgtOTMxMi0yMjIxODQ0NDMzMTQucG5nIiwid2lkdGgiOiI8PTEwMjQiLCJoZWlnaHQiOiI8PTU3NiJ9XV19.DsmMSnKtljCP7zEAAUsax4S3Lf9oASSChxWHmyPAT0g' | ||
| document.body.appendChild(img); |
| @@ -0,0 +1,7 @@ | |||
| function hijackGoogleLogo () { | |||
| let googleImage=document.querySelector('#hplogo') | |||
There was a problem hiding this comment.
this selector didn't work on my google version it would be interesting to see if it works on yours. can you make a screenshot of the element with this id ?
|
|
||
| <body> | ||
| <div id="klok"></div> | ||
| <script src="./showCurrentTime.js"> |
There was a problem hiding this comment.
I think you renamed your javascirpt file and forgot to update the src here therefore currently code doesn't run 😨
| <head> | ||
| <title>Klok :)</title> | ||
| <style> | ||
| div { |
There was a problem hiding this comment.
For the same reason explained above I believe it is better to use a css file and keep style in there instead of internal style tag
| const tijd = datum.toLocaleTimeString(); | ||
| document.querySelector('#klok').innerHTML = tijd; | ||
| } | ||
| setInterval(eenklok, 1000); No newline at end of file |
| function catWalk() { | ||
|
|
||
| setInterval(frame, 50) | ||
| function frame() { |
There was a problem hiding this comment.
Good work however I wouldn't declare a function inside of a function simply because no one can use the inner function outside the outer function.
I think it would be better to keep all functionality inside of catWalk function and then call catWalk with setInterval every 50 ms.
function catWalk() {
... all functionality
}
setInterval(catWalk, 50);| setInterval(frame, 50) | ||
| function frame() { | ||
|
|
||
| left = left > 1500 ? 0 : left; |
There was a problem hiding this comment.
What if the screen size is less than 1500? maybe look into getting the screen width using javascript for example using window.screen.width from webAPI
https://developer.mozilla.org/en-US/docs/Web/API/Screen/width
|
|
||
| } | ||
| } else { | ||
| img.style.left; |
There was a problem hiding this comment.
I'm not sure what did you want to do here it looks like you have read the img.style.left but didn't change or store it!

Review