• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking○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
○Conditionals○Demo: Combined Conditions Before FizzBuzz○Challenge: FizzBuzz Announcer○Switch Statement○Demo: Switch a Venue Sign○Challenge: Power Loadout○For Loops○While Loop○Break and Continue○Demo: Stop a Ticket Scan○Challenge: Number Guess Referee○Demo: Rainfall Routing Walkthrough○Exercise: Score Batch Report✦Module Quiz
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions○Demo: Ticket Pricing Walkthrough○Exercise: Shipping Labels with Defaults○Demo: Seat Ledger Closure Walkthrough○Exercise: Private Budget Ledger✦Module Quiz
○Arrays○Demo: 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○Demo: Charging Rack Restock Report○Exercise: Pantry Restock Plan✦Module Quiz
○Object Key-Value Pairs○Student Course Record○Object Methods and Destructuring○Map and Set Collections○Playlist Lookup○Number and BigInt Boundaries○Ticket Ledger○JSON○Math and Date○Date Basics○Demo: Supply Bin Audit○Exercise: Volunteer Shift Audit○Demo: Workshop Attendance Feed○Exercise: Parcel Scan Feed✦Module Quiz
○String Methods○Template Literals○Regular Expressions○Demo: Build a Reminder with Template Literals○Exercise: Format Pantry Pickup Messages○Demo: Validate a Time Slot with Regex○Exercise: Validate Storage Shelf Labels✦Module Quiz
○Higher-Order Functions○Callbacks○map, filter, and reduce○Demo: Student Register with map○Challenge: Student Register○Recursion○Project - Data Transform Pipeline○Demo: Handle an Error-First Callback○Exercise: Report Seat Reservation Results○Demo: Summarize Completed Bike Rides○Exercise: Summarize Delivered Parcels✦Module Quiz
○Demo: A notebook object○OOP Introduction○Demo: Independent workshop passes○Classes○Demo: An inherited notice○Inheritance○Demo: A private seat counter○Encapsulation○Demo: Equipment desk checkout○Project: Library Checkout✦Module Quiz
○Demo: Callback Timing○Exercise: Asynchronous JavaScript○Demo: Promise Rehearsal○Exercise: Promises and Async Await○Demo: Offline API Coordination○Exercise: Async API Simulation○Demo: Partial Results Bulletin○Project: Resilient Awards Feed○Demo: Retry a Local Delivery○Project: Reliable Score Delivery✦Module Quiz

Type Conversion

Mission: Scoreboard Engine

A price arrives as text. Convert it before multiplying by the quantity so the total remains a number.

Sometimes you need to change a value from one type to another. JavaScript supports both explicit (manual) and implicit (automatic) type conversion.

Explicit conversion

ConversionMethodExampleResult
To numberNumber()Number("42")42
parseInt()parseInt("3.9")3
parseFloat()parseFloat("3.9")3.9
Unary ++"42"42
To stringString()String(42)"42"
.toString()(42).toString()"42"
To booleanBoolean()Boolean(0)false
javascript
Number("100")   // 100
Number("")      // 0
Number("abc")   // NaN
Number(true)    // 1
Number(false)   // 0
Number(null)    // 0

Boolean(0)        // false
Boolean("")       // false
Boolean(null)     // false
Boolean("hello")  // true
Boolean(1)        // true

Implicit conversion (coercion)

JavaScript automatically converts types in certain contexts — sometimes in surprising ways:

javascript
"5" + 3      // "53"  — 3 converted to string
"5" - 3      // 2     — "5" converted to number
"5" * "3"    // 15    — both converted to numbers
true + 1     // 2     — true → 1
false + 1    // 1     — false → 0
null + 1     // 1     — null → 0
undefined + 1 // NaN

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.

Loading editor…
Press Run to execute your code.
READY
intercourses
javascript