QuickCheck: Breaking Down a UI Spec
How to add to the DOM
Section Exercises
Note: HW2 is out! Start this assignment early
By the end of this section, you should be able to:
classList.add
and
classList.remove
(more on this tomorrow!)Today, we will take what we've learned about JavaScript, events, the DOM, and animations to implement a game for a user to guess the correct number of Skittles in a jar having a certain color.
On a blank piece of paper, answer the following questions:
The elements of a page are nested into a tree-like structure of objects
Name | Description |
---|---|
document.createElement("tag") | creates and returns a new empty DOM node representing an element of that type |
// create a new <h2> node
let newHeading = document.createElement("h2");
newHeading.innerText = "This is a new heading!";
Note: Merely creating an element does not add it to the page
You must add the new element as a child of an existing element on the page...
When you have a parent DOM node, you can add or remove a child DOM node using the following functions.
Every DOM element object has these methods:
Name | Description |
---|---|
parentNode.appendChild(node) | places the given node at end of this node's child list |
parentNode.removeChild(node) | removes the given node from this node's child list |
let li = document.createElement("li");
li.innerText = "A list item!";
$("my-list").appendChild(li);
Given this starter skittles.html (and skittles.css), we will implement a "game" to fill a jar with Skittles and let a user see how quickly they can guess the correct number of Skittles in the with a certain color.
You can find the runnable example here.
.skittle
element.
When a user clicks the "Start" button:
A user can input numbers in the input box to guess how many Skittles of the current color are in the jar.
If they guess incorrectly, a 2-second message appears in the #results paragraph saying whether they guessed too high or too low, then is cleared. Their current guess count should be incremented by 1. If the user gives a negative number, this temporary message should be "You must enter a non-negative guess!"
When the user guesses correctly, the game is ended (see next slide for details).
When the game is ended:
Solution: JS
Given this starter dice-roller.html (and dice.css), we will implement an animated dice-rolling feature to roll a specified number of dice having a selected number of sides.
The #dice-area is the container to hold dice - each die should be a div with a "die" class added (this class is implemented for you in the provided CSS).
When the "Roll!" button is clicked, first the input box should be checked for valid input:
As soon as the dice are added, they should start "rolling" by displaying a random number (based on the number of sides) every 200 ms. Whenever dice are rolling, the dropdown, input box, and "Roll!" elements should be disabled.
Note: You can set the (boolean) disabled attribute for UI elements using the following syntax (assume a button with ID "my-btn" is defined):
$("my-btn").disabled = true;
When "Stop Roll!" is clicked, the values of all dice in the container should be added and displayed in the results paragraph and the button should be disabled. The previously-disabled elements should be re-enabled so a user can start a new dice roll (possibly with new number of dice and different die types).
Solution: JS