Skip to the content

Project: unit converter

JavaScript · Unit 4: Functions

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. 1. Complete the code

    Fill in toHours(min), which turns minutes into hours.

  2. 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 and toMinutes(2, 30) gives 150.

  3. 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. 4. Write the code

    Write convert(val, to). If to is "h", val is minutes and you hand back the hours; if not, val is hours and you hand back the minutes. When they don't say to, convert to minutes. Use the two functions that are already there.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →