Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a web page to play a simple guessing game. To start the game, the program

ID: 3538026 • Letter: C

Question

Create a web page to play a simple guessing game. To start the game, the program picks a secret number randomly between 1 and 100. The page contains a text box in which the player enters a number, and a button labeled Guess. When the player clicks on the Guess button, the program reports whether the guess is high, low, or equal to the secret number. If it is high or low, the player is told to guess again. If it is equal to the secret number, the game ends and a new game is started, with a new secret number.

Try to have JavaScript in an external file, and must use proper event-driven style if possible. The reports to the player are to be done by modifying the content of an element (such as a paragraph or div) in the page. Try not to use the document.write method is not to be used.

Add a guess limit. If the player uses up the maximum number of guesses, the page reports that the player has lost then reports what the secret number was, and starts a new game. The number of guesses left should be reported to the player on each round. Enjoy!!!!!!

Explanation / Answer

<!-- main html page. Save script.js separately as the external javascript file -->

<html>

<script src="script.js" >

</script>

<head>

<title>Guessing Game</title>

</head>

<body>


<input type="text" name="number" id="number">

<a href="#">Guess Number</a>

<p id="attempts"></p>



</body>

</html>

<!-- ------------------------------------------------------------- -->



//script.js

//Save this separately in the same folder as the main html page.


var randomnumber=0;

var attempts = 10;

var cur=10;

function init()

{

randomnumber=Math.floor(Math.random()*101);

document.getElementById('attempts').innerHTML=cur+" guesses left";

}



function check_num(){

var enterednumber=new Number(document.getElementById('number').value);

if(enterednumber==randomnumber )

{

alert('You entered the correct number '+randomnumber+'. Restarting game.');

cur=attempts;

init();

}else{

if(enterednumber>randomnumber && cur>1)

{

alert('Guess is higher than secret number');

cur=cur-1;

document.getElementById('attempts').innerHTML=cur+" guesses left";

}

if(enterednumber<randomnumber && cur>1)

{

alert('Guess is lower than secret number');

cur=cur-1;

document.getElementById('attempts').innerHTML=cur+" guesses left";

}

if(cur==1)

{alert('You ran out of guesses.Correct number was '+randomnumber+'. Restarting game.');

cur=attempts;

init();

}

}}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote