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: HelloWorldjs a simple API running on Node.js and using Express
var express requireexpress; use the express module and call it 'express'
var helloRequestsReceived ; initialize counter
var app express; create a new express server object called 'app'
app.useexpressurlencodedextended: false; allows us to parse ie get information from URLs
app.useexpressjson;
randomNumber allows us to parse ie 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 crossorigin resource sharing This just means content on a web page
can come from a domain other than the domain of that page.
app.usefunctionreq res, next
express.urlencodedextended: false
res.headerAccessControlAllowOrigin", ;
res.headerAccessControlAllowHeaders", "Origin, XRequestedWith, ContentType, Accept";
res.headerAccessControlAllowMethods", "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 ie http: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.poststartGame functionreqres
randomNumberGenerated Math.floorMathrandom;
console.logCreating game number' req.body.gameId The number to guess is randomNumberGenerated;
randomNumber reqbody.gameId randomNumberGenerated;
responseMessage APIMessage: new StringGame number' req.body.gameId 'started';
res.jsonresponseMessage;
;
app.getguessMade functionreqres
var numberToGuess randomNumberreqquery.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.logThe 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.jsonresponseMessage;
;
console.logListening on port ;
app.listen; And we're listening on port
I need help modifying it so it matches these criterias:
The user has five chances to guess the number.
The system checks that the user has entered a guess between and and
sends an appropriate error message if the user did not.
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 You have guesses left.
If the user guesses the number in 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.
If the user does not guess the number in 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
