In this course, you will use block-based programming, like on code.org, to create JavaScript that changes the html on a web page.
This "Introduction" has no tasks, only helpful information. Go to the first exercise whenever you're ready.
This exercise introduces the Html blocks to find and modify the text of html elements.
<p>The <u id="noun1">dog</u> <u id="verb">saw</u> the <u id="adjective">white</u> <u id="noun2">cat</u></p>
<u>
element to contain the word "mouse" instead of "dog".
noun1
)mouse
<u>
elements? If you can't think of some words, you can use the "get a random word" block from the Values menuNot only can we modify the contents of html elements, we can also change their properties, such as their colour. Different kinds of Values blocks provide ways of setting texts, numbers and colours.
<li>
) has a different id attribute so that we can refer to it from our program<ul>
<li id="banana">Banana</li>
<li id="apple">Apple</li>
<li id="strawberry">Strawberry</li>
</ul>
banana
)Not only can we modify existing html elements, we can also create new ones and set their text and properties.
<ul id="list">
<li>Apple</li>
</ul>
list
)This exercise consolidates the use of the blocks we learned about in the previous exercises.
<ul id="list"></ul>
) in the static html.
http://www.codeyourfuture.io
)<a>
inside of each <li>
<a>
you can use the <href>
attribute. Without this attribute, your link will not look like a linkJavaScript is often "event-driven". This means it is run in when the user interacts with the page (clicking, typing, etc.). Let's make it so every time we (as a "user") click on an "add an apple" button, it adds an apple to the list.
<ul>
) and a button to add apples to the list (<button>
) in the static html.
<ul id="list"></ul>
<button id="button">add an apple</button>
We can also get user input when a button is clicked. Let's have the browser say a word or sentence that the user enters.
<input />
) and a button to that says "speak" in the static html.
<input id="text" />
<button id="button">speak</button>
We're going to create a list of things to do.
<p>Things to do:</p>
<ul id="list"></ul>
<input id="text" />
<button id="button">add Todo Item</button>
<li>
with that value to the list. To do the visible outcome first, we're going to do these in reverse order.list
) and add a new <li>
with an arbitrary text value (for example "do cyf homework"). Check that this works.<li>
with the value of the input, using the "get the value of <input> with id" block.Anything that we could do previously as soon as run is clicked, we can now do as a response to clicking buttons or other elements in the static html.
A variable is a name into which a value can be stored. There are several reasons for which we might want to do this. The first is when we want to keep track of a value that changes over time. Let's keep track of the number of times a button is clicked.
<button>
) that has been clicked 0 times in the static html. <button id="button">0</button>
click_count
click_count
by 1; and change the text of the button to the new value of click_count.click_count
. (You can get this value by selecting the "click_count" block from the Variables menu)Note that it will be a common pattern that you first set a variable in the "at the start" block and then modify it in at "when the element with id ... is clicked" block. This first setting of the variable is called "initialisation".
Let's combine our variables to track state with some of the other blocks we have learned about. We would like to track how many times the user adds the word "sheep" and how many times they add any other word.
<p>There have been <span id="sheep_count">0</span> sheep 🐑 and <span id="other_count">0</span> others.</p>
<input id="text" />
<button id="button">add animal</button>
<span>
with id sheep_count
should increase by one.<span>
with id other_count
should increase by one.When we did the first Mad Libs exercise, we weren't able to make our own list of words to choose from but could only pick a category (noun, adjective, verb). We will now introduce a data structure called an Array, that represents a list of data (typically numbers or texts) in JavaScript. They can be created and used from the "Arrays" menu. Let's create our own list of words to select from.
<p>The <u id="noun">man</u> <u id="verb">saw</u> the <u id="adjective">white</u> <u id="animal">cat</u></p>
<u id="animal">
to a random animal.In this series of exercises, we have so far used one "create a new ... element" for each <li>
we want to create.
Another use for an array is when we want to do the same thing with each element in an array (for example, create a <li>
).
<ul id="list"></ul>
) in the static html.<li>
The most common something with all the items in an array is to use a loop, as we saw in the previous exercise. Sometimes, a loop just doesn't do what we need, for example if we wanted to make the list of fruit appear gradually.
<ul id="list"></ul>
<button id="button">reveal next fruit</button>
)<li>
with the name of the first fruit will appear.<li>
s with the text "undefined" are created. Let's fix that!<li>
element if the array is not empty. So we can add a "not" block in front of the "... is empty" block.Using the blocks from the previous exercise and a different kind of loop gives us an alternative way to loop through all the items of an array. Let's see a case where it might be useful.
<ul id="list"></ul>
) in the static html.<li>
and set the contents to a "get and remove the first item from the array" block, using the "fruits" array<li>
contain two things. The name and the emoji of the fruit.<li>
. In html, the <span>
element is typically used for such a purpose, because it doesn't add any extra meaning or formatting to the text (unlike others which say "this is a list item" or "this is a link")<span>
. Set the first's content to the "get and remove the first item from the array".We are now going to learn how to add items to, remove items from, and get the sum of, an array of numbers. We're going to keep a running sum of the last five numbers (this can be a way of keeping track of how a value trends over time, e.g. the last 5 times you weighed yourself, or the last 5 times you went running).
<p>Total of the last 5 numbers: <span id="total"></span> </p>
<input id="number" />
<button id="add_number">add value</button>
We are now going to use all the array blocks we have learned about. We're going to create a game where you roll dice and your goal is to not total more than 11.
<p>So far you have rolled:</p>
<ul id="list"></ul>
<button id="button_roll">Roll the dice</button>
<p>Total: <span id="total">0</span>. <span id="info">Keep playing!</span></p>
<button id="button_remove">Remove the last roll</button>
<button id="button_restart">Start again</button>
<span id="info">
<span id="info">
<span id="total">
<span id="total">
to the sum of the numbers in the "rolls" array.<span id="total">
to 0<li>
to the list and set the total. Instead, we are going to re-display the whole list from the array - you will see why in the next step<span id="info">
. For this project, you should provide the user with the ability to input names to an array, and a "select facilitator" button that selects and displays a random name
For this project, you should provide the user with the ability to input earnings and expenses and see the totals
Repeat the exercise to show a list of your favourite links. Use a loop so that there are only two "create a ... element" blocks (one for the <li>, one for the <a>). Outline what your solution needs to look like, and implement it step by step, clicking "run" at each iteration to check your work
The great thing about story books is that you never know what will happen when you turn the page!
An image carousel has an image with two buttons ("previous" and "next") that allow the user to cycle through a list of images.
You should first break down this problem into steps, then outline how each step will probably be solved (and how you can test that it does what you think it does), before creating any blocks
Here are some additional ideas of projects that can be accomplished with the current blocks (some newer blocks haven't been properly introduced yet) and might be suitable/adaptable as landing page enhancements. They all fit generally into the "add some button or event handlers that add or modify some html" category.