Introduction
In this project, you will learn how to build a simple "Guess the Coin" game. The game involves randomly selecting three cups out of nine cups to place coins in, and the player has to guess the correct cups by inputting the corresponding numbers.
👀 Preview

🎯 Tasks
In this project, you will learn:
- Implement the
findNumfunction to extract unique numbers from the user's input. - Implement the
randomCoinfunction to generate three non-repeating random numbers between 1 and 9. - Understand the project's directory structure and the provided files.
🏆 Achievements
After completing this project, you will be able to:
- Work with JavaScript functions and arrays.
- Use regular expressions to extract specific data from a string.
- Generate random numbers and ensure they are unique.
- Follow step-by-step instructions to complete a project.
Set Up the Project Structure
In this step, you will set up the project files and structure. Follow the steps below to complete this step:
Open the project folder. The directory structure is as follows:
├── css
├── images
├── index.html
└── js
├── findCoin.js
└── index.js
Where:
cssis the folder for style files.imagesis the folder for image files.index.htmlis the main page.js/index.jsis the JavaScript file for rendering and animating the coins.js/findCoin.jsis the JavaScript file that needs to be completed.
Click on Go Live button in the bottom right corner of WebIDE, to run the project.
Next, open "Web 8080" on the top of the VM and refresh it manually and you will see the page.
Implement the findNum Function
In this step, you will implement the findNum function in the js/findCoin.js file.
Open the
js/findCoin.jsfile.Locate the
findNumfunction:// Compose an array of the input values 1-9 function findNum(input_values) { // TODO const reg = /\d/g; const uniqueNumbers = []; const nums = input_values.match(reg) || []; nums.forEach((num) => { const parsedNum = parseInt(num); if (!isNaN(parsedNum) && !uniqueNumbers.includes(parsedNum)) { uniqueNumbers.push(parsedNum); } }); return uniqueNumbers; }The
findNumfunction takes aninput_valuesparameter and returns an array of unique numbers (1-9) found in the input.Implement the
findNumfunction by following these steps:- Use a regular expression
/\d/gto find all the digits in theinput_valuesstring. - Create an empty
uniqueNumbersarray to store the unique numbers. - Iterate through the matched numbers and convert them to integers using
parseInt. - Check if the parsed number is a valid number (not NaN) and not already in the
uniqueNumbersarray. - If the number is valid and unique, add it to the
uniqueNumbersarray. - Return the
uniqueNumbersarray.
- Use a regular expression
Implement the randomCoin Function
In this step, you will implement the randomCoin function in the js/findCoin.js file.
Locate the
randomCoinfunction:let randomCoin = () => { let randomNumArr = []; // TODO while (randomNumArr.length < 3) { const randomNumber = Math.floor(Math.random() * 9) + 1; if (!randomNumArr.includes(randomNumber)) { randomNumArr.push(randomNumber); } } return randomNumArr; };The
randomCoinfunction should generate an array of 3 non-repeating random numbers between 1 and 9.Implement the
randomCoinfunction by following these steps:- Create an empty
randomNumArrarray to store the random numbers. - Use a
whileloop to generate 3 unique random numbers between 1 and 9. - Inside the loop, generate a random number using
Math.floor(Math.random() * 9) + 1. - Check if the generated number is not already in the
randomNumArrarray. - If the number is unique, add it to the
randomNumArrarray. - Once the
randomNumArrarray has 3 unique numbers, return the array.
- Create an empty
After completing these steps, the "Guess the Coin" game should be fully functional. A gif of all the functions completed is as follows:

Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



