Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this javascript code and I cannot get the understanding of what should be

ID: 3747055 • Letter: I

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 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("<!DOCTYPE html><html><head><title>Cool Smiley Faces</title></head>");

    response.write(encode(firstSmiley));

    response.write("<p><a href="/">Back</a></p>");

    response.end();

This is the index.js

The random.js

*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!!!!!

Explanation / Answer

/**
* 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('./node_modules/cool-ascii-faces')
var encode = require('./helpers/encode');
var rand = require('./helpers/random')

var server = http.createServer(function (request, response) {
if (request.url == '/') {
    var now = new Date();
    response.write("<!DOCTYPE html><html><head><title>Node.js Greetings</title></head>");
    response.write("<p>Current time is " + now + "</p>");
    response.write("<a href="/smiley">Click here for a smiley face</a>");
    response.end();
} else if (request.url == '/smiley') {
    var smileyArray = cool.faces;

    var firstSmiley = smileyArray[rand(smileyArray.length)];
    console.log(firstSmiley)
    //var firstSmiley = smileyArray[0];
    response.write("<!DOCTYPE html><html><head><title>Cool Smiley Faces</title></head>");
    response.write(encode(firstSmiley));
    response.write("<p><a href="/">Back</a></p>");
    response.end();
} else {
    response.write("<!DOCTYPE html><html><head><title>404 ERROR</title></head>");
    response.write("<p> 404 ERROR: Page doesn't exist.</p>");
    response.end();
}

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


/-------------------------------------------------------------------------------------------------------/

/**
* encode.js
* This file contains a function that
* converts special symbols to
* HTML encoded values`
* For exmaple:
*   Special symbol:
*   will be converted to &#9824
* For another example:
*   Special symbol sequence:
*   will be converted to &#9737&#9735&#9737
*/
module.exports = function(originalSmiley) {
    var length = originalSmiley.length;
    var encodedSmiley = "";
    for (var i = 0; i < length; i++) {
        encodedSmiley += "&#" + originalSmiley.charCodeAt(i);
    }
    return encodedSmiley;
}


/--------------------------/

/**
* 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);
}

Description:

file system is in the below format:

|-----------helpers|---------encode.js

                        |---------random.js

|-----------node_modules                                                 

|-----------index.js

How to use the anonymous function:
Example:

Consider a file named 'file1' which has function defined as

function f1(x) // one way of defining a function //

{

return 2*x;

}

var f1 = function(x) // Anonymous way of calling a function by referencing to a variable //

{

return 2*x;

}

By either way as mentioned above, function calling happens as below:

var y = f1(3) // where x =3 in f1 //

If only an anonymous has to be exported to a file from another file, then it follows the same way as the second way mentioned above, but only difference is instead of defining the function in the same file, we're are defining it some other file( i.e., var y = require("<file path from it has to be exported>")