Question

Question 1: What port is this application running on? Question 2: How many function definitions do...

Question 1: What port is this application running on?

Question 2: How many function definitions do you see in this program? List the function names. If it’s an anonymous function write the function definition.

Question 3: How many global variables are there in this program? What are they? (Hint: Some are programmer defined using keyword var and some provided by JavaScript language implicitly.)

var http = require('http');

// function to calculate rabbit reproduction using fibonacci sequence
function fibonacci(n) {
    if (n < 2)
        return 1;
    else
        return
fibonacci(n - 2) + fibonacci(n - 1);
}

var server = http.createServer(function (request, response) {
    if (request.method == 'GET' && request.url == '/') {
        response.write('<!DOCTYPE html>');
        response.write('<html>');
        response.write('<head><title>Rabbit Population Forcast</title></head>');
        response.write('<body>');
        response.write('<h1>Rabbit Population Forecast</h1>')
        response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
        response.write('<ul>');
        response.write('<li><a href="/1m">1 month population</a></li>');
        response.write('<li><a href="/10m">10 months population</a></li>');
        response.write('<li><a href="/30m">30 months population</a></li>');
        response.write('<li><a href="/44m">44 months population</a></li>');
        response.write('</ul>');
        response.write('</body>');
        response.write('</html>');
        response.end();
    } else if (request.method == 'GET' && request.url == '/1m') {
        response.write('<!DOCTYPE html>');
        response.write('<html>');
        response.write('<head><title>Rabbit Population Forcast</title></head>');
        response.write('<body>');
        console.time('1m_timer');
        response.write('<p style="color: red">Pairs of rabbits after 1 month is: ' + fibonacci(1) + '</p>');
        console.timeEnd('1m_timer')
        response.write('<h1>Rabbit Population Forecast</h1>')
        response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
        response.write('<ul>');
        response.write('<li><a href="/1m">1 month population</a></li>');
        response.write('<li><a href="/10m">10 months population</a></li>');
        response.write('<li><a href="/30m">30 months population</a></li>');
        response.write('<li><a href="/44m">44 months population</a></li>');
        response.write('</ul>');
        response.write('</body>');
        response.write('</html>');
        response.end();
    } else if (request.method == 'GET' && request.url == '/10m') {
        response.write('<!DOCTYPE html>');
        response.write('<html>');
        response.write('<head><title>Rabbit Population Forcast</title></head>');
        response.write('<body>');
        console.time('10m_timer');
        response.write('<p style="color: red">Pairs of rabbits after 10 months is: ' + fibonacci(10) + '</p>');
        console.timeEnd('10m_timer');
        response.write('<h1>Rabbit Population Forecast</h1>')
        response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
        response.write('<ul>');
        response.write('<li><a href="/1m">1 month population</a></li>');
        response.write('<li><a href="/10m">10 months population</a></li>');
        response.write('<li><a href="/30m">30 months population</a></li>');
        response.write('<li><a href="/44m">44 months population</a></li>');
        response.write('</ul>');
        response.write('</body>');
        response.write('</html>');
        response.end();
    } else if (request.method == 'GET' && request.url == '/30m') {
        response.write('<!DOCTYPE html>');
        response.write('<html>');
        response.write('<head><title>Rabbit Population Forcast</title></head>');
        response.write('<body>');
        console.time('30m_timer');
        response.write('<p style="color: red">Pairs of rabbits after 30 months is: ' + fibonacci(30) + '</p>');
        console.timeEnd('30m_timer');
        response.write('<h1>Rabbit Population Forecast</h1>')
        response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
        response.write('<ul>');
        response.write('<li><a href="/1m">1 month population</a></li>');
        response.write('<li><a href="/10m">10 months population</a></li>');
        response.write('<li><a href="/30m">30 months population</a></li>');
        response.write('<li><a href="/44m">44 months population</a></li>');
        response.write('</ul>');
        response.write('</body>');
        response.write('</html>');
        response.end();

Homework Answers

Answer #1

Ans 1) Port no. 80
Explanation: As given in first line the application is using http protpcol. As no specific port number is mentioned, the
application run on default port number of http i.e port no. 80.

Ans 2)
There are two functions are defined in the given code.
1. fibonacci (named function)- Fibonacci is the first named function in this program, which is recursive in nature and gives the fibonacci series.
2. server (anonymous function)- var server is a variable name to which an anonymous function is assigned.

Ans 3)
There are total 3 variables that are declared in this code snippet. They are-
http: variable created to load and cache javascript module named 'http'
n: variable created to calculate fibonacci series of a number 'n'.
server: this variable is created for anonymous function.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions