Project: unit converter
JavaScript · Unit 4: Functions
- Full plan
- JavaScript
- Project
- 4 exercises
You're going to build a time converter out of small functions.
Each one does a single conversion and hands back its result; at the end, one uses them all.
const toSeconds = (min) => min * 60;
console.log(toSeconds(3));Prints
180
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Complete the code
Fill in
toHours(min), which turns minutes into hours.2. Write the code
Write
toMinutes(hrs, mins), which hands back how many minutes there are in total. If nobody says the minutes, they are 0:toMinutes(2)gives 120 andtoMinutes(2, 30)gives 150.3. Find the case that fails
label(min)should hand back "long" if it is 60 minutes or more, and "short" if it isn't. Which call makes it fail?4. Write the code
Write
convert(val, to). Iftois "h",valis minutes and you hand back the hours; if not,valis hours and you hand back the minutes. When they don't sayto, convert to minutes. Use the two functions that are already there.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.