Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App

Crack-The-Code Game using JavaScript

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

It is quite easy to develop with some simple mathematics. A player has to guess the 3 numbers in order to win this game by using 5 simple hints. This is going to be a very interesting game. This game is built using simple mathematics in JavaScript.

Prerequisites: Basic knowledge of some front-end technologies like HTML, CSS, JavaScript, and Bootstrap.

View of Crack-The-Code

Filename: index.html

HTML
<!doctype html> <html lang="en">  <head>     <meta charset="utf-8">     <meta name="viewport" content="width=device-width,         initial-scale=1, shrink-to-fit=no">      <title>Crack-The-Code Game</title>      <!-- Bootstrap CDN starts-->     <link rel="stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"         crossorigin="anonymous">      <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"          crossorigin="anonymous">     </script>      <script src= "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"         crossorigin="anonymous">     </script>          <script src= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"          crossorigin="anonymous">     </script>     <!-- Bootstrap CDN ends -->      <!-- Font Awesome kit --- for icons -->     <script src="https://kit.fontawesome.com/788fc8cac3.js"          crossorigin="anonymous">     </script>      <!-- Google font CDN for Yeon Sung font -->     <link rel="preconnect" href="https://fonts.gstatic.com">     <link href= "https://fonts.googleapis.com/css2?family=Yeon+Sung&display=swap"         rel="stylesheet">     <!-- Google font CDN ends -->      <!-- Internal CSS -->     <style>         * {             font-family: 'Yeon Sung', cursive;         }          input {             width: 60px;             height: 40px;             border-radius: 5px;             text-align: center;         }     </style> </head>  <body>      <!--Popup Modal -->     <div class="modal fade" id="popup" tabindex="-1"          role="dialog" aria-labelledby="popupLabel"          aria-hidden="true">                  <div class="modal-dialog" role="document">             <div class="modal-content">                 <div class="modal-header">                     <h5 class="modal-title text-danger                          font-weight-bold" id="popupLabel">                         Pop Up                     </h5>                                          <button type="button" class="close"                          data-dismiss="modal" aria-label="Close">                         <span aria-hidden="true">&times;</span>                     </button>                 </div>                 <div class="modal-body">                     <h3 id="result" class="text-center"></h3>                 </div>                 <div class="modal-footer">                     <button type="button" class="btn                          btn-secondary" data-dismiss="modal">                         Close                     </button>                 </div>             </div>         </div>     </div>     <!-- Popup Modal -->      <div class="container my-4">         <h1 class="text-center my-4"><i class="fas                  fa-unlock-alt"></i> Crack The Code</h1>          <!-- 3 input fields -->         <div class="text-center">             <input type="text" id="b1">             <input type="text" id="b2">             <input type="text" id="b3">             <br />             <!-- Button to check result -->             <button class="btn btn-secondary mt-4"                  onclick="myfunc();">                 <i class="fas fa-key"></i> Check             </button>         </div>         <hr>          <!-- Hints starts -->         <div id="hints" class="row m-auto">             <div id="hintOne">                 <div class="card m-4" style="width: 18rem;">                     <div class="card-header card-title">                         <h5 class="card-title text-center">                             <i class="far fa-lightbulb"></i>                             Hint #1                         </h5>                     </div>                     <div class="card-body">                         <p id="h1" class="text-center"></p>                          <h5 class="text-center">                             One Number is correct and                              well placed                         </h5>                     </div>                 </div>              </div>             <div id="hintTwo">                 <div class="card m-4" style="width: 18rem;">                     <div class="card-header card-title">                         <h5 class="card-title text-center">                             <i class="far fa-lightbulb"></i>                              Hint #2                         </h5>                     </div>                     <div class="card-body">                         <p id="h2" class="text-center"></p>                          <h5 class="text-center">                             One Number is correct but wrong placed                         </h5>                     </div>                 </div>             </div>             <div id="hintThree">                 <div class="card m-4" style="width: 18rem;">                     <div class="card-header card-title">                         <h5 class="card-title text-center">                             <i class="far fa-lightbulb"></i>                              Hint #3                         </h5>                     </div>                     <div class="card-body">                         <p id="h3" class="text-center"></p>                          <h5 class="text-center">                             Two numbers are correct but                              wrong placed                         </h5>                     </div>                 </div>             </div>             <div id="hintFour">                 <div class="card m-4" style="width: 18rem;">                     <div class="card-header card-title">                         <h5 class="card-title text-center">                             <i class="far fa-lightbulb"></i>                              Hint #4                         </h5>                     </div>                     <div class="card-body">                         <p id="h4" class="text-center"></p>                          <h5 class="text-center">                             Nothing is Correct                         </h5>                     </div>                 </div>             </div>             <div id="hintFive">                 <div class="card m-4" style="width: 18rem;">                     <div class="card-header card-title">                         <h5 class="card-title text-center">                             <i class="far fa-lightbulb"></i>                              Hint #5                         </h5>                     </div>                     <div class="card-body">                         <p id="h5" class="text-center"></p>                          <h5 class="text-center">                             One Number is correct but                              wrong placed                         </h5>                     </div>                 </div>             </div>         </div>         <!-- Hints ends -->      </div> </body> <!-- JavaScript file included --> <script src="script.js"></script>  </html> 

Filename: script.js

JavaScript
// Number to decide the game digit i.e.  // game work on 2 digit, 3 digit or n // digit of number. const digit = 100;  // Set random numbers. The task of user  // is to find these numbers. let num1 = Math.floor(Math.random() * digit); let num2 = Math.floor(Math.random() * digit); let num3 = Math.floor(Math.random() * digit);  // Hints are generated here onwards. // Hint 1 let h1_a = Math.floor(Math.random() * digit); let h1_b = Math.floor(Math.random() * digit); let h1_c = num3;  // Hint 2 let h2_a = Math.floor(Math.random() * digit); let h2_b = Math.floor(Math.random() * digit); let h2_c = num2;  //Hint 3 let h3_a = num2; let h3_b = num1; let h3_c = Math.floor(Math.random() * digit);  // Hint 4 let h4_a = Math.floor(Math.random() * digit); let h4_b = Math.floor(Math.random() * digit); let h4_c = Math.floor(Math.random() * digit);  // Hint 5 let h5_a = Math.floor(Math.random() * digit); let h5_b = Math.floor(Math.random() * digit); let h5_c = num1;  // Hint generation ends  // Putting hints to index.html page document.getElementById('h1').innerHTML =      `<input type="text" id="b1" value="${h1_a}     " readonly> <input type="text" id="b1" value="     ${h1_b}" readonly> <input type="text" id="b1"      value="${h1_c}" readonly>  `;  document.getElementById('h2').innerHTML =      `<input type="text" id="b1" value="${h2_a}     " readonly> <input type="text" id="b1"      value="${h2_b}" readonly> <input type="text"      id="b1" value="${h2_c}" readonly>  `;  document.getElementById('h3').innerHTML =      `<input type="text" id="b1" value="${h3_a}     " readonly> <input type="text" id="b1"      value="${h3_b}" readonly> <input type="text"      id="b1" value="${h3_c}" readonly>  `;  document.getElementById('h4').innerHTML =      `<input type="text" id="b1" value="${h4_a}     " readonly> <input type="text" id="b1"      value="${h4_b}" readonly> <input type="text"      id="b1" value="${h4_c}" readonly>  `;  document.getElementById('h5').innerHTML =      `<input type="text" id="b1" value="${h5_a}     " readonly> <input type="text" id="b1"      value="${h5_b}" readonly> <input type="text"      id="b1" value="${h5_c}" readonly>  `;  // Function to check whether game is solved or not function myfunc() {      // Getting value of user though input fields.     let a = document.getElementById('b1').value;     let b = document.getElementById('b2').value;     let c = document.getElementById('b3').value;      // Checking whether input fields is blank or not     if (a != '' && b != '' && c != '') {         if (a == num1 && b == num2 && c == num3) {              // Outputting this message to index.html             // page with id result.             $('#result').html('You Crack it.????');              // Opening popup modal             $('#popup').modal('toggle');         } else {              // Outputting this message to index.html             // page with id result.             $('#result').html('Try once again.????');              // Opening popup modal             $('#popup').modal('toggle')         }     }     else {                  // Outputting this message to index.html         // page with id result.         $('#result').html('Fill all fields.????');          // Opening popup modal         $('#popup').modal('toggle');     } } 

Step to run the program:

Run the index.html file by opening it in any browser.

Output:

When player left the input field blank.

When answer is wrong

When you crack it


A

asmitsirohi
Improve
Article Tags :
  • Technical Scripter
  • JavaScript
  • Web Technologies
  • HTML
  • Bootstrap
  • Technical Scripter 2020
  • JavaScript-Questions

Similar Reads

    Create a snake game using HTML, CSS and JavaScript
    Snake Game is a single-player game where the snake gets bigger by eating the food and tries to save itself from the boundary of the rectangle and if the snake eats their own body the game will be over.Game Rules:If the snake goes out of the boundary or eats its own body the game will be over.Prerequ
    4 min read
    Design Dragon's World Game using HTML CSS and JavaScript
    Project Introduction: "Dragon's World" is a game in which one dragon tries to save itself from the other dragon by jumping over the dragon which comes in its way. The score is updated when one dragon saves himself from the other dragon.  The project will contain HTML, CSS and JavaScript files. The H
    6 min read
    Word Guessing Game using HTML CSS and JavaScript
    In this article, we will see how can we implement a word-guessing game with the help of HTML, CSS, and JavaScript. Here, we have provided a hint key & corresponding total number of gaps/spaces depending upon the length of the word and accept only a single letter as an input for each time. If it
    4 min read
    Build a Memory Card Game Using HTML CSS and JavaScript
    A memory game is a type of game that can be used to test or check the memory of a human being. It is a very famous game. In this game, the player has some cards in front of him and all of them facing down initially. The player has to choose a pair of cards at one time and check whether the faces of
    6 min read
    Create a Simon Game using HTML CSS & JavaScript
    In this article, we will see how to create a Simon Game using HTML, CSS, and JavaScript. In a Simon game, if the player succeeds, the series becomes progressively longer and more complex. Once the user is unable to repeat the designated order of the series at any point, the game is over.Prerequisite
    5 min read
    Create a Minesweeper Game using HTML CSS & JavaScript
    Minesweeper is a classic puzzle game that challenges your logical thinking and deduction skills. It's a great project for developers looking to improve their front-end web development skills. In this article, we'll walk through the steps to create a Minesweeper game using HTML, CSS, and JavaScript.
    4 min read
    Whack-a-Mole Game using HTML CSS and JavaScript
    Whack-A-Mole is a classic arcade-style game that combines speed and precision. The game is set in a grid of holes, and the objective is to "whack" or hit the moles that randomly pop up from these holes. In this article, we are going to create Whack-a-Mole using HTML, CSS and JavaScript.Preview Image
    3 min read
    Simple HTML CSS and JavaScript Game
    Tap-the-Geek is a simple game, in which the player has to tap the moving GeeksForGeeks logo as many times as possible to increase their score. It has three levels easy, medium, and hard. The speed of the circle will be increased from level easy to hard. I bet, it is very difficult for the players to
    4 min read
    Design Hit the Mouse Game using HTML, CSS and Vanilla Javascript
    In this article, we are going to create a game in which a mouse comes out from the holes, and we hit the mouse with a hammer to gain points. It is designed using HTML, CSS & Vanilla JavaScript.HTML Code:First, we create an HTML file (index.html).Now, after creating the HTML file, we are going to
    5 min read
    Create a 2D Brick Breaker Game using HTML CSS and JavaScript
    In this article, we will see how to create a 2D Brick Breaker Game using HTML CSS & JavaScript. Most of you already played this game on your Mobile Phones where you control a paddle to bounce a ball, aiming to demolish a wall of bricks arranged at the top of the screen. 2D Brick Breaker Game is
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences