Skip to content
dreamcode
dreamcode
Map
Velocity Formatter
Section challenge · BeginnerJavaScript
Reward: +40 XP
PROBLEM

Velocity Formatter

Write an arrow function formatVelocity that takes two parameters: value (a number) and unit (a string). If unit is not provided, it should default to 'km/h'. The function should return a template literal string in the format: {value} {unit}. If value is negative, return 'Invalid speed'.

Examples
formatVelocity(120, "mph")
"120 mph"
formatVelocity(50)
"50 km/h"
solution.js
JAVASCRIPT
Saved as you type

Tests

0 of 4 passing
  • mph formatting
    formatVelocity(120, "mph")
    expected "120 mph"
  • default unit formatting
    formatVelocity(50)
    expected "50 km/h"
  • negative value check
    formatVelocity(-10)
    expected "Invalid speed"
  • zero speed formatting
    formatVelocity(0, "m/s")
    expected "0 m/s"
On the line
+40 XP
Pass all 4 tests to claim it.