3. Show Prices in the Store Currency
A marketplace serving Egypt and the Gulf stores every price as an integer in the currency's minor unit (cents, piasters, fils) to avoid floating-point errors. The front end turns those integers into display strings.
Currencies do not all have two decimals: the Japanese yen has none and the Kuwaiti dinar has three. Refunds are negative amounts. The output must be identical on every device, so the team uses its own formatting rules instead of the browser's locale settings.
Your task:
Write formatPrice(amountMinor, currencyCode) and return the display string.
Rules: - Symbol and decimals per currency: USD "$" 2, EUR "€" 2, GBP "£" 2, EGP "E£" 2, SAR "SAR " 2, JPY "¥" 0, KWD "KD " 3. - Any other code: the code followed by a space as the symbol, with 2 decimals (for example "CHF 10.00"). - Use "," as the thousands separator and "." as the decimal separator. Always show every decimal digit ("$0.05", "$12.50"). - Negative amounts put "-" before the symbol: "-E£12.50".
Notes: - Work with integers (division and remainder), not with floating-point division.
Examples
Example 1
- Input
amountMinor = 123456
currencyCode = "USD"
- Output
"$1,234.56"
- Explanation
- 123456 cents is 1,234 dollars and 56 cents.
Example 2
- Input
amountMinor = 1234567
currencyCode = "KWD"
- Output
"KD 1,234.567"
- Explanation
- The Kuwaiti dinar has 1000 fils, so three decimals.
Example 3
- Input
amountMinor = -1250
currencyCode = "EGP"
- Output
"-E£12.50"
- Explanation
- A refund: the minus sign goes before the symbol.
Solution
Write formatPrice. Tab indents (Esc then Tab leaves the editor), Ctrl/⌘ + Enter runs. Each run has a 3-second limit. Your code is saved in this browser.
Test Results
Click "Run Code" to test your solution.