Please help.The html and css code + instructions are in the image. The server code is below Thank you var express = require('express'); //we must have express installed! var app = express(); var idCounter = 0; // we will assign every round of game play a unique ID var gameInfo = {}; // an empty JS object, later it's going to store the code for each end-user var port = 3000; app.post('/post', (req, res) => { //print info to console console.log("New express client"); console.log("Query received: "); console.log(JSON.parse(req.query['data'])); //populate a header response res.header("Access-Control-Allow-Origin", "*"); var queryInfo = JSON.parse(req.query['data']); if (queryInfo['action'] == 'generateWord') { //are we asked to generate a word? idCounter++; var nameID = queryInfo['name'] + idCounter; generateWord(nameID); var jsontext = JSON.stringify({ 'action': 'generateWord', 'nameID': nameID, 'len': gameInfo[nameID][0].length, 'msg': 'New word generated!!!' }); res.send(jsontext); } else if (queryInfo['action'] == "evaluate") { //are we asked to evaluate a guess? var [correct, counter, attemptWord] = makeGuess(gameInfo[queryInfo['name']], queryInfo['letterGuess'], queryInfo['guessWord']); gameInfo[queryInfo['name']][1] = counter /* the response will have 6 parts: request action, whether won or not, number of exact matches, number of colors that are correct, number of wrong colors, and the answer if the game ended */ var jsontext = JSON.stringify({ 'action': 'evaluate', 'correct': correct, 'attemptWord': attemptWord, 'num_errors': counter }); console.log(jsontext); res.send(jsontext); } else { res.send(JSON.stringify({ 'msg': 'error!!!' })); } }).listen(3000); console.log("Server is running!"); /* * Evauate the client's guess * The parameters to this function are: * input, letterGuess, attemptWord * input is the server's stored info for this client * - input[0] contains the client's secret WORD * - input[1] contains the client's ERROR COUNT * letterGuess is the client attempted guess (it is a letter) * attemptWord records the client's current progress on the word (it is an array) * * The function returns an array [correct, num_errors, attemptWord] * correct is a BOOLEAN that indicates if the user's guess is correct * num_errors is count of the user's ERRORS thus far * attemptWord is an updated record of the client's current progress on the word */ function makeGuess(input, letterGuess, attemptWord){ //you will write this code! //input[0] contains this user's secret word //input[1] contains this user's error count //letterGuess is the letter that the user has guessed //attemptWord is a log of letters the user has guessed correctly. //STEP 1. Cycle thru the user's secret word //Assess if and where letterGuess appears in this word //If it appears in the word, update the ltters within //attemptWord accordingly //STEP 2. Calculate the user's error count, //if it is the case that letterGuess does not exist in //input[0] //RETURN an array containing a BOOLEAN, //a NUMBER, and attemptWord (which is an array) //Your return statement will look something //like this: //return [correct, num_errors, attemptWord] } function generateWord(clientName) { var possibleWords = [ ["R", "A", "D", "I", "O"], ["T","E","A","M","W","O","R","K"], ["W","E","B","D","E","S","I","G","N"], ["E","D","U","C","A","T","I","O","N"], ["C","H","O","C","O","L","A","T","E"], ["U","N","I","V","E","R","S","I","T","Y"] ] //generate word var index = //... Finish this line!! Select an element from possibleWords at random. gameInfo[clientName] = [possibleWords[index], 0]; //store the secret word and error count for this user! }
Please help.The html and css code + instructions are in the image. The server code is below
Thank you
var express = require('express'); //we must have express installed!
var app = express();
var idCounter = 0; // we will assign every round of game play a unique ID
var gameInfo = {}; // an empty JS object, later it's going to store the code for each end-user
var port = 3000;
app.post('/post', (req, res) => {
//print info to console
console.log("New express client");
console.log("Query received: ");
console.log(JSON.parse(req.query['data']));
//populate a header response
res.header("Access-Control-Allow-Origin", "*");
var queryInfo = JSON.parse(req.query['data']);
if (queryInfo['action'] == 'generateWord') { //are we asked to generate a word?
idCounter++;
var nameID = queryInfo['name'] + idCounter;
generateWord(nameID);
var jsontext = JSON.stringify({
'action': 'generateWord',
'nameID': nameID,
'len': gameInfo[nameID][0].length,
'msg': 'New word generated!!!'
});
res.send(jsontext);
} else if (queryInfo['action'] == "evaluate") { //are we asked to evaluate a guess?
var [correct, counter, attemptWord] = makeGuess(gameInfo[queryInfo['name']], queryInfo['letterGuess'], queryInfo['guessWord']);
gameInfo[queryInfo['name']][1] = counter
/* the response will have 6 parts: request action, whether won or not, number of exact matches,
number of colors that are correct, number of wrong colors, and the answer if the game ended */
var jsontext = JSON.stringify({
'action': 'evaluate',
'correct': correct,
'attemptWord': attemptWord,
'num_errors': counter
});
console.log(jsontext);
res.send(jsontext);
} else {
res.send(JSON.stringify({ 'msg': 'error!!!' }));
}
}).listen(3000);
console.log("Server is running!");
/*
* Evauate the client's guess
* The parameters to this function are:
* input, letterGuess, attemptWord
* input is the server's stored info for this client
* - input[0] contains the client's secret WORD
* - input[1] contains the client's ERROR COUNT
* letterGuess is the client attempted guess (it is a letter)
* attemptWord records the client's current progress on the word (it is an array)
*
* The function returns an array [correct, num_errors, attemptWord]
* correct is a BOOLEAN that indicates if the user's guess is correct
* num_errors is count of the user's ERRORS thus far
* attemptWord is an updated record of the client's current progress on the word
*/
function makeGuess(input, letterGuess, attemptWord){
//you will write this code!
//input[0] contains this user's secret word
//input[1] contains this user's error count
//letterGuess is the letter that the user has guessed
//attemptWord is a log of letters the user has guessed correctly.
//STEP 1. Cycle thru the user's secret word
//Assess if and where letterGuess appears in this word
//If it appears in the word, update the ltters within
//attemptWord accordingly
//STEP 2. Calculate the user's error count,
//if it is the case that letterGuess does not exist in
//input[0]
//RETURN an array containing a BOOLEAN,
//a NUMBER, and attemptWord (which is an array)
//Your return statement will look something
//like this:
//return [correct, num_errors, attemptWord]
}
function generateWord(clientName) {
var possibleWords = [
["R", "A", "D", "I", "O"],
["T","E","A","M","W","O","R","K"],
["W","E","B","D","E","S","I","G","N"],
["E","D","U","C","A","T","I","O","N"],
["C","H","O","C","O","L","A","T","E"],
["U","N","I","V","E","R","S","I","T","Y"]
]
//generate word
var index = //... Finish this line!! Select an element from possibleWords at random.
gameInfo[clientName] = [possibleWords[index], 0]; //store the secret word and error count for this user!
}
Step by step
Solved in 3 steps with 1 images