thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

16.04 · Input & output / Practice · 5 min

Convert a typed age

The program treats entered digits as text. Convert them before doing arithmetic.

Activity 4 of 7 in Input & output

All courses

JavaScript Basics / 16.04

Convert a typed age

Not started

The program treats entered digits as text. Convert them before doing arithmetic.

More options

Your lesson progress

Reading & walkthrough: not startedGuided practice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob compares a text card marked 18 with a numeric token marked 18, then checks a next-age result of 19.
Rob’s comic strip · Input & output

Convert a supplied text reply before calculating

Work through 3 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

3 steps · Saved work · Instant feedback

YOUR TURN

Add to numeric input

In nextAge, convert rawAge to a number before adding 1. Keep the calls for "18", "0" and " 20 ". Print 19, 1 and 21. Assume the supplied strings contain valid nonnegative whole-number ages; this task does not implement input validation.

Expected output
19
1
21

Keep the starting inputs. Print these lines in this order, without extra output.

Edit JavaScript, then run it. Check Challenge shows which output checks pass or need a fix. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
function nextAge(rawAge) {
  return rawAge + 1;
}
console.log(nextAge("18"));
console.log(nextAge("0"));
console.log(nextAge(" 20 "));

Need a nudge?

What type arrives?

A typed line is text. Number(rawAge) converts a known numeric string to a number; adding 1 afterward performs arithmetic.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Convert known numeric text before adding a numeric amount.