In this tutorial, we’re going to write a number guessing game using JavaScript.

Other tutorials of this series.

Notes for this tutorial:

Task for this tutorial: Write a JavaScript program to

- Generate a random secret number ranged from 0 to 100
- The program will tell the player the range and the player has to guess the number
- The range will be narrowed until the player make a correct guess

Genrate a random number between 0 and 100 (inclusive)

1 | //Math.random() returns a number between 0 (inclusive) and 1 (exclusive) |

Math operators

1 | let num = 2+3; //add |

Functions

1 | function multiply(a, b) { //a and b are parameters |

- parameter
- return value
- scope of variables

Error (exception) handling

1 | function someFun() { |

Check for equality

1 | const a = "foo"; |

Algorithm

- Binary search
- Time complexity \(O(\log n)\)

The source code of the program in this tutorial on GitHub.