Students read an input with .value, react as they type with the input event, find that values are text and use Number() to add, and make a calculator or converter of their own.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 15 min
Read the unit primer first. This is the Python input() lesson with a twist: there is no program waiting, so the page reacts to an event. input fires on every keystroke; click on a button reads the box once. The big idea is that .value is always text, as input() was in Python: the same "45" surprise. The bug has no red error, so students must read the answer. Number("abc") is NaN; isNaN(x) checks for it, used in the Make example. The playground cannot send a form, on purpose: use a button and an event, not a <form>.
Text versus number: "4" + "5" is "45". Show the page, ask what + does to text, and only then show Number().
| What you will see | A question to ask | The fix |
|---|---|---|
| The sum shows 45, or the text joined. | Is the value turned into a number first? | Wrap each value: Number(a.value). |
| Cannot read properties of null (reading value). | Does the input id match the selector? | Match the id exactly, with #. |
| The box seems to do nothing. | Is the listener on the box or the button, and is it the right event? | Use "input" on the box, or "click" on the button. |
| NaN is shown. | Was the box empty or full of words? | Check with isNaN and show a friendly message. |
One possible solution for each task. Other correct programs are fine.
<input id="a" value="4"> +
<input id="b" value="5">
<button id="add">Add</button>
<p id="sum"></p>
<script>
const a = document.querySelector("#a");
const b = document.querySelector("#b");
document.querySelector("#add").addEventListener("click", function () {
document.querySelector("#sum").textContent = Number(a.value) + Number(b.value);
});
</script>
<textarea id="msg" rows="3" cols="30" placeholder="Type here"></textarea>
<p><span id="count">0</span> characters</p>
<p id="warning"></p>
<script>
const msg = document.querySelector("#msg");
const count = document.querySelector("#count");
const warning = document.querySelector("#warning");
msg.addEventListener("input", function () {
count.textContent = msg.value.length;
if (msg.value.length > 20) {
warning.textContent = "Too long!";
warning.style.color = "red";
} else {
warning.textContent = "";
}
});
</script>
<h1>Tip calculator</h1>
<input id="amount" placeholder="Bill in dollars">
<button id="go">Work out a 10% tip</button>
<p id="answer"></p>
<script>
const amount = document.querySelector("#amount");
const answer = document.querySelector("#answer");
document.querySelector("#go").addEventListener("click", function () {
const bill = Number(amount.value);
if (isNaN(bill)) {
answer.textContent = "Please type a number.";
} else {
answer.textContent = "Tip: $" + (bill * 0.1).toFixed(2);
}
});
</script>
Stretch: Make the result appear live, with no button at all, using the input event on each box.
Support: Give the greeting page and ask students to change only the words, then add a second box.
Why is "4" + "5" equal to "45"?
The value of an input is text, and + joins text. Number() turns it into a number first.
Next lesson: lists of elements: making new elements and adding them to the page.