topskill.dev
CoursesTracks
0
All courses/Introduction to JavaScriptType Conversion
Variables and Operators
4 / 9
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking○Missing values and deliberate emptiness○Inspect Values in the Console○Inspect the Queue Report○Trace a Faulty Calculation○Repair the Score Calculation✦Module Quiz
○Declaring Variables○let and const in Practice○Operators●Type Conversion○Group and Convert Expense Inputs○Calculate the Event Expense Total○Track a Changing Points Tally○Adjust a Ticket Price✦Module Quiz
○Comparisons and strict equality○Truthiness and combined conditions○Function inputs and results○Conditionals○Combined Conditions Before FizzBuzz○Challenge: FizzBuzz Announcer○Switch Statement○Switch a Venue Sign○Challenge: Power Loadout○For Loops○While Loop○Break and Continue○Stop a Ticket Scan○Challenge: Number Guess Referee○Rainfall Routing Walkthrough○Score Batch Report✦Module Quiz
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions○Ticket Pricing Walkthrough○Shipping Labels with Defaults○Seat Ledger Closure Walkthrough○Private Budget Ledger✦Module Quiz
○Arrays○Student Scoreboard○Challenge: Student Course Scoreboard○Array Mutation Methods○Array Slice and Concat○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every○Charging Rack Restock Report○Pantry Restock Plan✦Module Quiz
○Object Key-Value Pairs○Student Course Record○Object Methods and Destructuring○Object copies and optional properties○Map and Set Collections○Playlist Lookup○Number and BigInt Boundaries○Ticket Ledger○JSON○Throwing and catching errors○Math and Date○Date Basics○Supply Bin Audit○Volunteer Shift Audit○Workshop Attendance Feed○Parcel Scan Feed✦Module Quiz
○String Methods○Template Literals○Regular Expressions○Build a Reminder with Template Literals○Format Pantry Pickup Messages○Validate a Time Slot with Regex○Validate Storage Shelf Labels✦Module Quiz
○Higher-Order Functions○Callbacks○map, filter, and reduce○Student Register with map○Challenge: Student Register○Recursion○Project - Data Transform Pipeline○Extend the inventory report○Handle an Error-First Callback○Report Seat Reservation Results○Summarize Completed Bike Rides○Summarize Delivered Parcels✦Module Quiz
○A notebook object○OOP Introduction○Independent workshop passes○Classes○An inherited notice○Inheritance○A private seat counter○Encapsulation○Equipment desk checkout○Project: Library Checkout✦Module Quiz
○Callback Timing○Asynchronous JavaScript○Promise Rehearsal○Promises and Async Await○Offline API Coordination○Async API Simulation○Partial Results Bulletin○Project: Resilient Awards Feed○Retry a Local Delivery○Project: Reliable Score Delivery✦Module Quiz

Type Conversion

A price arrives as text. Convert it into a number before calculating the order total, then format the result as a message.

Convert deliberately

Number("42") returns the number 42. It does not change the original string; store the returned value under a new name. typeof lets you check the difference even when the Console prints the same digits.

javascript
const deliveryText = "8";
const delivery = Number(deliveryText);
console.log(deliveryText, typeof deliveryText);
console.log(delivery, typeof delivery);
console.log(delivery + 2);

String(42) goes the other way and returns "42". A template literal also turns inserted values into text for display. Keep the calculation numeric until you need that final message.

Boolean(0) and Boolean("") return false; Boolean(1) and Boolean("hello") return true. Boolean conversion asks whether a value is truthy, not what its text means: even Boolean("false") is true because that string is not empty.

Watch what + receives

With two numbers, + adds. With a string operand, it joins text instead:

javascript
console.log("8" + 2);
console.log(Number("8") + 2);

Other arithmetic operators can convert strings automatically: "8" * 2 produces 16. That automatic conversion is called coercion. Use explicit Number() here so the code says what kind of value the calculation expects; getting a numeric product alone does not prove you converted the input yourself.

Not all text is a usable number. Number("abc") gives NaN (“not a number”), while an empty string converts to 0. This exercise supplies valid numeric text. Checking unknown input is a separate job.

The starter has a checkpoint after every placeholder. Complete the conversion before calculating the total, and keep the dollar sign in the final display string rather than in the numeric price.

Edit script.js, Run to inspect each checkpoint, then Submit to check the completed tasks. The supplied probes do not need rewriting.

Your Task

  1. Set priceStr to "29"; the provided probe will show priceStr: 29 (string).
  2. Convert priceStr with Number() into price; the provided probe will show price: 29 (number).
  3. Set quantity to 3; the provided probe will show quantity: 3.
  4. Calculate total as price * quantity; the provided probe will show total: 87 (number).
  5. Log Total: $87 using the numeric total.
Hint 1
1 / 3
HINT 1

Use Number() to convert a string to a number.

HINT 2

Use String() or template literals to convert a number to a string.

HINT 3

Use Boolean() to convert any value to true/false.

Files
  • ⋄script.js
Loading editor…
Press Run to execute your code.