Javascript - Number Guessing Game
Submitted by razormist on Friday, August 17, 2018 - 19:32.
In this tutorial we will create a Number Guessing Game using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...
There you have it we successfully created a Number Guessing Game using Javascript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting started:
This is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted;"/>
- <br />
- <div class="form-inline">
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will generate a random number that will be guested by the user to start the game. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.- $(document).ready(function(){
- var guesses_list = "";
- var number = Math.floor(Math.random() * 101);
- var count = 1;
- $("#guess").keypress(function (e) {
- if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
- return false;
- }
- });
- $('#btn_guess').on('click', function(){
- if($('#guess').val() == ""){
- alert("Please enter something");
- }else{
- CheckGuess();
- }
- })
- $('#btn_restart').on('click', function(){
- RestartGame();
- });
- function CheckGuess(){
- var guess = $('#guess').val();
- if(count == 1) {
- $('#guesses').css('border', '1px dotted #000');
- $('#guesses').css('padding', '10px');
- }
- guesses_list += " "+guess;
- $('#guesses').text(guesses_list);
- if(guess == number){
- $('#btn_guess').attr('disabled', 'disabled');
- $('#guess').attr('disabled', 'disabled');
- $('#result').html("Congratulations, You Win!");
- $('#result').attr('class', 'alert alert-success');
- $('#btn_restart').show();
- $('#tips').html('');
- }else if(count == 10){
- $('#result').html('GameOver!');
- $('#result').attr('class', 'alert alert-danger');
- $('#tips').html('');
- GameOver();
- }else{
- $('#result').html("Not Close Enough!");
- $('#result').attr('class', 'alert alert-danger');
- if(guess < number){
- $('#tips').html("That's too low, Guess Higher");
- }else if(guess > number){
- $('#tips').html("That's too high, Guess Lower");
- }
- }
- count++;
- $('#guess').val('');
- }
- function GameOver(){
- $('#btn_guess').attr('disabled', 'disabled');
- $('#guess').attr('disabled', 'disabled');
- $('#btn_restart').show();
- }
- function RestartGame(){
- count = 1;
- number = Math.floor(Math.random() * 101);
- $('#guesses').css('border', 'none');
- $('#btn_guess').removeAttr('disabled');
- $('#guess').removeAttr('disabled');
- guesses_list = "";
- $('#guesses').text('');
- $('#tips').html('');
- $('#result').removeAttr('class');
- $('#result').html('');
- $('#btn_restart').hide();
- }
- });
Add new comment
- 476 views