Site icon DataFlair

JavaScript Project – Guess Random Number Game Console Version

Full Stack Web Development Courses with Real-time projects Start Now!!

Program 1

//Guess Random Number Game(Console Application)
"use strict"
const ps=require("prompt-sync")
const prompt=ps({sigint:true})
let compchoice,userchoice
let choice
do
{
    compchoice=Math.floor(Math.random()*100)
    console.log("-----------Guess Random Number Game-------------")
    console.log("You have to guess a number(0-99) if you guess in 5 Chance you are winner !")
    let i=0
    let count=5
    while(true)
    {
        i++
        count--
        userchoice=parseInt(prompt("Enter Your Number: "))
        if(userchoice>compchoice)
            console.log("Wrong your number is grater , now " + count + " chance left")
        else if(userchoice<compchoice)
            console.log("Wrong your number is less , now " + count + " chance left")     
        else if(userchoice==compchoice)
        {
            console.log("Wow.... Congrats you won the game... in " + i + " attampet !")     
            break
        }   
        if(count==0)
        {
            console.log("Oops you lose the game all chances over....!")
            console.log("Correct Number is : "+compchoice)
            break;
        }
    }
    choice=prompt("Do you want to play more (yes/no): ").toLowerCase()
}while(choice=="yes")    
    console.log("Thanks for playing. Bye Bye ....!")

 

Exit mobile version