Average Speed
Programming Basics · JavaScript
TASK
Problem
Given distance `s` and time `t`, print average speed `s / t`.
EXAMPLE
Example
150 3
50.0
LIMITS
Constraints
s ≥ 0; t > 0
LEARN
Theory for this problem
+
### Average Speed
Integer division separates higher decimal/time units, while `%` returns the remainder. These two operations let a program isolate digits or split a total number of seconds/minutes into components.
NEED HELP?
Hints
+
['Decide which part comes from division and which part comes from the remainder.', 'Match each input value type to the operation performed on it: JavaScript string and numeric behavior differ significantly.']
ANSWER
Solution
+
const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
let [s, t] = input.trim().split(/\s+/).filter(Boolean).map(Number);
let result = (s / t);
console.log(Number.isInteger(result) ? result.toFixed(1) : result);