Question
I have this javascript code and I cannot get the understanding of what should be done with the import anonymous function. If you could do
I have this javascript code and I cannot get the understanding of what should be done with the import anonymous function. If you could do this and explain
Part One: A Simple hello web site with cool ascii faces
________________________________________________________________________
Project Description:
You are given a Node.js project based on our Hello Node project in Unit 01 Lab Part One.
The instructor should provide you the source code in unit05_lab.zip file in BlackBoard Unit 05 Lab sections.
On the top level, there are three JavaScript files (encode.js, index.js, and random.js) and a folder named node_modules. Here is a brief description of what they are:
index.js the entry point of this application, very similar to app.js in Unit 01 Lab
encode.js source file that exports a programmer defined function for HTML encoding
random.js source file that exports a programmer defined function for generating random numbers.
Right now you will get the same smiley face all the time. Your job is 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;
// this is the code that needs to be changed to pick a random smiley face
var firstSmiley = smileyArray[0];
response.write("
response.write(encode(firstSmiley));
response.write("
Back
");response.end();
This is the 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 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 firstSmiley = smileyArray[0]; response.write("Cool Smiley Faces "); response.write(encode(firstSmiley)); 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");
The 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); } The encode.js
/** * encode.js * This file contains a function that * converts special symbols to * HTML encoded values` * For exmaple: * Special symbol: * will be converted to ♠ * For another example: * Special symbol sequence: * will be converted to ☉☇☉ */ module.exports = function(originalSmiley) { var length = originalSmiley.length; var encodedSmiley = ""; for (var i = 0; i < length; i++) { encodedSmiley += "" + originalSmiley.charCodeAt(i); } return encodedSmiley; }
*All questions are based on the original index.js source code provided by the instructor.
Question 1: How would you modify the index.js file if the two helper JavaScript files were placed in a file structure like below? (Yellow ones are folders. Blue ones are files.)
unit05_lab
|-----------helpers|---------encode.js
|---------random.js
|-----------node_modules
|-----------index.js
The Helper and node_modules are folders
Thanks!!!!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started