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

Checking your account…

04.04 · Numbers / Practice · 4 min

Add numeric text

The two prices are correct, but JavaScript joined their text. Make the total a calculation.

Activity 4 of 8 in Numbers

All courses

JavaScript Basics / 04.04

Add numeric text

Not started

Convert two numeric strings before adding their values.

More options
Rob discovers quoted prices joined as text, converts the values, and checks a numeric total.
Rob’s comic strip · Numbers

Calculate with numbers and numeric text

Work through 4 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)

4 steps · Saved work · Instant feedback

YOUR TURN

Add numeric text

The two prices arrive as strings. Convert both values to numbers before adding them. Keep priceOne and priceTwo in the expression. Print just 15 for the starting values.

Expected output
15

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
const priceOne = "10";
const priceTwo = "5";
const total = priceOne + priceTwo;

console.log(total);

Need a nudge?

Check the kind of value

Quotation marks make each price a string. + joins strings exactly as written.

Convert both inputs

Use Number(priceOne) and Number(priceTwo) in the addition expression. Keep the total numeric until it is printed.

Reveal a possible solution
One possible solution
example.jsjavascript · example
const priceOne = "10";
const priceTwo = "5";
const total = Number(priceOne) + Number(priceTwo);

console.log(total);

Changing only one operand to a number still leaves a string in the addition. Try "2.85" and "2.15": the same expression should print 5. The output check does not replace reviewing your use of variables.

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

Run a small calculation, inspect the output, then change the input and try again.