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

Two parts project (Please leave comments in the code so I can learn where the ch

ID: 3746276 • Letter: T

Question

Two parts project (Please leave comments in the code so I can learn where the changes has been made) Thanks

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.zipfile in BlackBoard Unit 05 Lab sections. There are many files and folders in this project seen below:


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.

If you run this application now, you should see something like this:

When you click on the bottom link you will get a smiley face:

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. Here are some screenshots:

  

  

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

   response.write(encode(firstSmiley));

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

   response.end();

You need to submit only the modified index.jsfile.

Note:

There are many free Node.js projects out there. The instructor downloaded the cool-ascii-faces from this site: https://github.com/maxogden/cool-ascii-faces.

There are three imports at the top of index.js file:

var http = require(‘http’);

This is Node.js built-in module for handling HTTP request and response which you will need for any web applications.

var cool = require(‘cool-ascii-faces’);

This statement imports a third-party module called ‘cool-ascii-faces’. You should put your third-party modules under a specially named folder ‘node-modules’. (Chapter 4 will explain this in detail.)

var encode = require(‘./encode’);

This statement imports a programmer defined module (something you wrote).

________________________________________________________________

                                               

Part Two: Short answer questions

*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 s---encode.js random.js helpers node modules index.is

Explanation / Answer

Ans:

/**

* index.js

* This program is a server-side JavaScript program that

*/

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

* 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

* 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>")

Part two:

To modify the index.js file since index.js imports code from encode.js and random.js we need to require both the file as object in our index.js file. For that we can write two statements in the begning of the index.js file.

var encode = require("./helpers/encode");

var random = require("./random");

After creating the object of these two files we can use the required function and data according to the requirement of what we want to do from index.js file in the project.

To use the method and properties of those two files we have to export those method and properties so that we can import it in index.js file.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote