Question: Here is my current code: / / HelloWorld 1 . js - a simple API running on Node.js and using Express var express = require

Here is my current code: // HelloWorld1.js - a simple API running on Node.js and using Express
var express = require('express'); // use the express module and call it 'express'
var helloRequestsReceived =0; // initialize counter
var app = express(); // create a new express() server object called 'app'
app.use(express.urlencoded({extended: false})); // allows us to parse (i.e., get information from) URLs
app.use(express.json());
randomNumber =[]// allows us to parse (i.e., get information from) JSON
// app.use() configures the middleware server object. It is called each time a request is sent to the server.
// It contains code for general configuration of the server.
// In this case we're setting up CORS (cross-origin resource sharing). This just means content on a web page
// can come from a domain other than the domain of that page.
app.use(function(req, res, next){
express.urlencoded({extended: false})
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
next();
});
// app.get() instructs the application what to do when an HTTP GET request is made to the API.
// In this case, the code only runs if you use the route /sayhello (i.e., http://127.0.0.1/sayhello).
// And the code just increments the counter, sends a line of output to the console, and sends a line of
// output to the browser.
app.post('/startGame', function(req,res){
randomNumberGenerated = Math.floor(Math.random()*100)+1;
console.log('Creating game number' +req.body.gameId +'. The number to guess is'+ randomNumberGenerated);
randomNumber [req.body.gameId]= randomNumberGenerated;
responseMessage ={ APIMessage: new String('Game number' + req.body.gameId + 'started')};
res.json(responseMessage);
});
app.get('/guessMade', function(req,res){
var numberToGuess = randomNumber[req.query.gameId];
var numberGuessed= req.query.guessMade;
var guessed= false; // boolean variable that returns to the html program.
// default is false, set to true if the user guessed correctly
console.log('The number guessed was'+ numberGuessed +'. The number to guess is'+ numberToGuess);
if (numberGuessed < numberToGuess){
var outMessage = "The guess of"+ numberGuessed +"is too low!"
} else if (numberGuessed > numberToGuess){
var outMessage = "The guess of"+ numberGuessed +"is too high!"
} else {
var outMessage = "The guess of"+ numberGuessed +"is correct"
guessed = true;
}
responseMessage ={ APIMessage: outMessage, Guessed: guessed };
res.json(responseMessage);
});
console.log("Listening on port 8080");
app.listen(8080); // And we're listening on port 8080
I need help modifying it so it matches these criterias:
The user has five (5) chances to guess the number.
2. The system checks that the user has entered a guess between 1 and 100, and
sends an appropriate error message if the user did not.
3. The system should display the current guess number and the number of guesses
left every time the user enters a guess. Example message: This is guess
number 2. You have 3 guesses left..
4. If the user guesses the number in 5 guesses or less:
a. Display an appropriate congratulations message
b. Disable the Enter a Guess and Reset the Game buttons
c. Enable the Start the Game button.
5. If the user does not guess the number in 5 guesses or less:
a. Display an appropriate you lost message
b. Display the answer the number the user was trying to guess
c. Disable the Enter a Guess and Reset the Game buttons
d. Enable the Start the Game button

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!