number guessing game

// Guess the number game game rules: 4 random numbers between 0-9 are randomly generated by the system, each bit is different // The number and position are correct and return A only if the number is correct and the position is incorrect return B let readline = require ('readline-sync'); // Determine if the number is repeated The function receives a parameter which is an array let isRepeat = function(arr){ let length = arr.length; for(let i=0;i<length;i++) { for(let j=i+1;j<length;j++){ if(arr[i] === arr[j]){ return 1; // means there is repetition} } } return 0; // means no Repeat} // A function for the computer to generate 4 different numbers let randomNum = function(){ let num; // used to generate random numbers between 0-9 let comNum = []; // the final generated number to be returned Number, use an array to load while(true){ comNum.length = 0; for(let i=0;i<4;i++){ num = Math.floor(Math.random()*10); comNum.push( num); } if(!isRepeat(comNum)){ return comNum; } } } // the main function of the program let main = function(){ let guessNum; // the number used to receive user input let a = 0,b = 0,chance = 10; // a represents the number of A b represents the number of B chance represents the chance to guess let saySth = ['Come on', 'It's a little bit closer', 'I'm almost right', 'lady What's going on', 'Organize the idea']; // Encouragement statement let comNum = randomNum();// Call the randomNum function to generate a random number while(chance){ // There are two cases to exit this infinite loop: one is that the player guesses right, and there is still chance left, and the other is that the chance is used up and becomes 0 console. log('Please enter the number you want to guess'); guessNum = readline.question(''); if(guessNum.length!=4){ console.log('The number of digits you entered is incorrect'); }else if(isNaN(Number(guessNum))){ console.log('There is something wrong with the number you entered!'); }else{ guessNum = guessNum.split(''); // Convert the string to an array let repeat = isRepeat(guessNum); if(repeat === 0){ // Indicates that the numbers entered by the user are not repeated, and then the comparison is started for(let i=0;i<guessNum.length;i++){ for(let j=0;j<comNum.length;j++){ // Determine if the numbers are equal if(guessNum[i] == comNum[j]){ // Determine if the subscripts are equal if(i === j ){ a++; }else{ b++; } } } } // If a is 4, it means you guessed correctly if(a === 4){ console.log('Congratulations, you guessed correctly'); break ; // out of the while loop}else{ chance--; // chance decrements console.log(`${a}A${b}B`); if(chance != 0){ let index = Math.floor (Math.random()*saySth.length); console.log(`You have ${chance} remaining, ${saySth[index]}`); } // Make sure to reset a and b a = 0; b = 0; } } else{ console.log ('The number you entered is repeated'); } } } if(chance === 0){ console.log('Unfortunately, you no longer have a chance!'); console.log('The number generated by the computer is :',comNum); }else{ console.log('Brother is really good, you guessed it right'); console.log('Game over! Thank you for playing'); } } main();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325127143&siteId=291194637