Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. Give that person the feedback, that is, say it to the person.

Answered: 1 week ago