Question: Using node.JS Write to modify this program by randomly choosing a smiley face from an array of smiley faces. You need to import the anonymous

Using node.JS

Write to modify this program by randomly choosing a smiley face from an array of smiley faces. You need to import the anonymous function from source file random.js.

After you have modified the program, you should get different smiley faces when you click the link.

Hint #1: You need to import the function from random.js by using require() function. (See how encode.js is imported for clue.)

Hint #2: You need to replace the following highlighted code with some new code that will randomly pick a smiley face from the array named smileyArray.

} else if (request.url == '/smiley') {

var smileyArray = cool.faces;

****** var firstSmiley = smileyArray[0];******

response.write("Cool Smiley Faces");

response.write(encode(firstSmiley));

response.write("

Back

");

response.end();

You need to submit only the modified index.js file.

index.js

/** * index.js * This program is a server-side JavaScript program that * simulates a Hello web site which runs on port 3333 * The web site displays the current time of the server. */ var http = require('http'); var cool = require('cool-ascii-faces'); var encode = require('./encode'); var random = require('./random'); var

var server = http.createServer(function (request, response) { if (request.url == '/') { var now = new Date(); response.write("Node.js Greetings"); response.write("

Current time is " + now + "

"); response.write("Click here for a smiley face"); response.end(); } else if (request.url == '/smiley') { var smileyArray = cool.faces; var SmileyFace = ["( .-. )", "( .o.)", "( ` )", "( )", "( )", "( _ )", "( )", "( )", "(\\/)(,,,)(\\/)", "(_)", "(-)", "()", "()", "()", "( _)", "(')", "()", "( )", "( )", "( )", "(_)", "()", "()", "(_)", "()", "( )", "( )", "( _)", "('-')"]; Random rand = new Random(); var no = rand.nextInt(6);

response.write("Cool Smiley Faces"); response.write(encode(SmileyFace); response.write("

Back

"); response.end(); } else { response.write("404 ERROR"); response.write("

404 ERROR: Page doesn't exist.

"); response.end(); }

}); server.listen(3333); console.log("Server is running at port: 3333");

random.js

/** * random.js * This file contains a function that * generates a random number between 0 and n-1 */ module.exports = function(n) { return Math.floor(Math.random() * n); }

note: I added the array of smileFace but still something missing

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 Databases Questions!