JavaScript · Level 1 Basics

Numbers & Prices

Do arithmetic with JavaScript numbers and turn a price stored in cents into a label with exactly two decimals.

  • + - * / and % on numbers
  • Decimals are not always exact (0.1 + 0.2)
  • toFixed(2) returns a string with two decimals

Numbers use + - * / %. The trap: decimals are not exact, so shops store prices in cents and format them only for display. toFixed(2) returns a string with two decimals.

0.1 + 0.2;                // 0.30000000000000004
7 % 3;                    // 1 (remainder)
const cents = 450;
(cents / 100).toFixed(2); // "4.50"

Write formatPrice(cents) that returns the price as a label with a dollar sign and two decimals, e.g. formatPrice(1250) returns $12.50.

solution.js
Test Suites Pending
Output