Hailstone Numbers

A classic number series.

Skills: InputOutputVariablesIf StatementsLoops
1%

This project is a chance for you to practice the skills you learnt in the previous three projects.

You'll need to use all of the skills to write the code. Here's some references to help:

Revision

If you'd like to revise then you can do this revision quiz to test your knowledge of input, output, if statements, and loops.

Brief

Write a program that calculates the hailstone sequence for a number that the user inputs. The hailstone algorithm takes a number and continues one of these two operations until the number is 1:
  • If the number is even, divide it by two.
  • If the number is odd, times it by three and add one.

For example, if the user puts in 5 then your program should ouput:

5, 16, 8, 4, 2, 1

Use the code below to get you started. Copy and paste the HTML into your own file. You do not need to write any more HTML to complete this project.

You can use the JavaScript below if you need help to get started. It contains comments explaining each step of this program.

If you can, try to complete the program without looking at it.