Find the largest of three numbers

“Biggest of three”, a White belt challenge from the CurlyScript Dojo. It uses what Grade 4: Making Decisions: if, else & Friends teaches, and 5 tests check your answer.

The challenge

Write biggest(a, b, c) that returns the largest of three numbers. Use if statements or comparisons, not Math.max.

Try it

Write your answer below, then press Check my answer. Your code runs right here in your browser, and the tests tell you what works.

What the tests check

  • biggest(1, 2, 3) gives 3
  • biggest(9, 2, 3) gives 9
  • biggest(4, 8, 6) gives 8
  • biggest(-1, -5, -3) gives -1
  • biggest(5, 5, 2) gives 5

Hints

Stuck? Open one hint at a time. Try again after each one.

Hint 1

Keep a variable best = a, then replace it if b or c is bigger.

A solution

There’s always more than one right way. Read it, make sure you understand it, then try writing it yourself without looking.

Show one possible solution
Solution
function biggest(a, b, c) {
  let best = a;
  if (b > best) best = b;
  if (c > best) best = c;
  return best;
}

Learn what you need

This exercise uses what Grade 4: Making Decisions: if, else & Friends teaches. Read those notes first if any of it is new.

Practice makes it stick

CurlyScript is a free game that teaches JavaScript from zero: short lessons, an explanation after every answer, and Dojo challenges like this one.

Start learning