Interacting with the DOM is a crucial part of building a website. If we cannot interact with the DOM and the javascript we write cannot be used in a browser. Connecting javascript to the browser opens up a new world of possibilities where only the imagination is the limiting factor.
-
Open a new js file and start by declaring an array that contains 10 strings. These strings should be of book titles you have read (or made up) and be lowercase without spaces or special characters so that you can use these later as Id's. (Example: Harry Potter's - The Chamber of Secrets ->
harry_potter_chamber_secrets). -
Create a basic html file called index.html and use it to load the js file, confirm the console.log show the array. (This is for debugging and making sure everything is in order. Delete it later when you're done :))
-
Make a function (or functions) that generate a
ulwithlielements for each book ID in the array using a for loop. -
Make an object containing information for each book. Each item (object) in this object should have the book ID you thought up in point 1 as a key, and it should have at least the following fields: title, language and author.
-
Now change the function you used to display the book ID's in a list to take the actual information about the book from the object and display that. Make sure you choose the right html elements for each piece of info, for instance, a heading for the title.
-
Beautify your html page with css, add sources and alts to each of the images.
-
Download book covers for each book, construct a new Object which has as keys the bookId's again, and as value the path to the image source (e.g.
{"harry_potter_blabla": "./img/harry_potter_blabla.jpg", ...}). Now loop over these entries (hint:Object.keys(objectName)gives you an array containing the keys). Then write a function which places an image at the correspondinglielement. Remember that Objects are not ordered, so you cannot guarantee that the first key is the firstlielement. (Hint: you could give eachliitem anidtag by modifying the function you made before)
Let's create a page where a user writes his name in an input element. The user then clicks a button. The user will now receive a spirit animal name, fx Benjamin - The fullmoon wolf.
Create an input element, a button and a tag to display the spirit animal into.
When the user clicks the button, get the name the user wrote in the input field.
Now we can get the users name, next step is to add the spirit animal string and display that the users name, a dash and then the spirit animal. Fx Name: Peter: Peter - The crying butterfly For creating the spirit animal create an array with 10 string representing spirit animals. Now get a random item in the array that will represent the spirit animal.
Now a user tells us that he wants a new spirit animal. No problem we say. Let's create functionality where a user can press a button and then get a new spirit animal.
What if the user clicks the generate new spirit animal and there is no text in the input?
Give the user the possibility to select when the spirit animal should be created. Should it be when the user clicks the button or the user hovers the input field or when text is written in the input field?
How can we give a user multiple options to select from in html? Maybe time for google!
An example is: A user select that she only wants to generate a spirit animal when the input is hovered. That means that if the user writes her name in the input and clicks the button nothing happens. BUT when she hovers the input, NOW a new spirit animal is generated.
Find a student to give feedback using this site: https://hyf-peer-review.herokuapp.com/ The feedback should be given after the homework has been handed in, preferably the latest two days after.
To help you get started we have created some resources about giving feedback. Find them here: https://github.com/HackYourFuture-CPH/curriculum/tree/master/review
Go over your homework one last time:
- Does every file run without errors and with the correct results?
- Have you used
constandletand avoidedvar? - Do the variable, function and argument names you created follow the Naming Conventions?
- Is your code well-formatted (see Code Formatting)?
If you can answer yes to the above questions then you are ready to hand if the homework:
- Find the hyf-homework git repo (forked from here)
- Add your homework files in the Javascript/javascript2/week1 folder
- To finish the homework, use the link in the top of your classes slack channel.
🎉

