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.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
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.