The page shows 45 instead of 9. There is no red error. Click the line that is wrong.
ModifyChange the code on purpose and check the result.
Modify
Numbers, not text.
Need a hint?
The page is adding text. What does + do with text?
Another hint
Wrap each value in Number(...): Number(a.value) + Number(b.value).
Show the expected resultWhat it should look like. Your words can be different.
Decide as they type.
Need a hint?
An if inside the handler can look at how many characters there are.
Another hint
Add a paragraph for the warning, and if (msg.value.length > 20) { … } to fill it.
Show the expected resultWhat it should look like. Your words can be different.
Try it yourselfYour edits are saved in this browser.
Make the Add button show 9 for 4 + 5.
This ticks when your page has all of the above.
Show Too long! in red when the message has more than 20 characters.
This ticks when your page has all of the above.
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
A calculator of your own
Make a small tool that reads a number from a box and shows an answer: a tip calculator, a temperature converter (°C to °F), an age-in-days counter, a unit converter. Check for a bad number with isNaN(...), as the page on this lesson does.
Try it yourselfYour edits are saved in this browser.
Info
Where this leads
You can now read what people type. Next: build a list on the page.
For teachers
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. About 50 minutes.
I can react on every keystroke with the input event.
I can turn typed text into a number with Number().
Before class
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>.
Running the lesson
Predict: ask when the value is read.
Run: ask students to type quickly and say when the number changes.
Investigate: ask what + does to two pieces of text.
Modify: ask what Number("5") gives, and what Number("hello") gives.
Make: ask each student what could go wrong if someone types words.
The hard part (on purpose)
Text versus number: "4" + "5" is "45". Show the page, ask what + does to text, and only then show Number().
When students get stuck
The sum shows 45, or the text joined. Ask: Is the value turned into a number first? Fix: Wrap each value: Number(a.value).
Cannot read properties of null (reading value). Ask: Does the input id match the selector? Fix: Match the id exactly, with #.
The box seems to do nothing. Ask: Is the listener on the box or the button, and is it the right event? Fix: Use "input" on the box, or "click" on the button.
NaN is shown. Ask: Was the box empty or full of words? Fix: Check with isNaN and show a friendly message.
Stretch and support
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.
Where it leads
Next lesson: lists of elements: making new elements and adding them to the page.
What each step is for
Investigate: .value is the text typed so far, and an input event fires on every keystroke; because it is text, turn it into a number with Number() before doing sums.
Modify: Number() turns typed text into a number so sums work; an if inside the handler reacts to what was typed.
a calculator of your own: Goals only: read an input, turn it into a number, work something out and show it on the page.