Nyxynyx
Nyxynyx

Reputation: 63647

Why must this be passed in as an Array and not a String?

I'm following a node.js tutorial from a book called Node Web Developments.

Problem: In one part of the code, an array [] containing HTML code is passed from mult-node.js into the required function htutil.page() as the 3rd argument, and the 2 values returned by exports.navbar() and exports.page() in htutil.js but I cannot figure out why it must be an array and not a long string? I don't see any code in htutil.page() that extracts the array into a long string where it will be displayed on a HTML page.

htutil.js

var url = require('url');

exports.loadParams = function(req, res, next) {
    req.requrl = url.parse(req.url, true);
    req.a = (req.requrl.query.a && !isNaN(req.requrl.query.a))
        ? new Number(req.requrl.query.a)
        : NaN;
    req.b = (req.requrl.query.b && !isNaN(req.requrl.query.b))
        ? new Number(req.requrl.query.b)
        : NaN;
        if(next) next();
}

exports.navbar = function() {
    return ["<div class='navbar'>",
            "<p><a href='/'>Home</a></p>",
            "<p><a href='/mult'>Multiplication</a></p>",
            "<p><a href='/square'>Square's</a></p>",
            "<p><a href='/factorial'>Factorial's</a></p>",
            "<p><a href='/fibonacci'>Fibonancci's</a></p>".
            "</div>"].join('\n');
}

exports.page = function(title, navbar, content) {
    return ["<html><head><title>{title}</title></head>",
            "<body><h1>{title}</h1>",
            "<table><tr>",
            "<td>{navbar}</td><td>{content}</td>",
            "</tr></table></body></html>"].join('\n');
            .replace("{title}", title, "g")
            .replace("{navbar}", navbar, "g")
            .replace("{content}", content, "g");
}

mult-node.js

var htutil = require('./htutil');
exports.get = function(req, res) {
    res.writeHead('200', {'Content-Type': 'text/html'});
    var result = req.a * req.b;
    res.end(
        // THIS IS THE FUNCTION WHERE THE ARRAY OF HTML CODE IS PASSED INTO
        htutil.page('Multiplication', htutil.navbar(), [
            (!isNaN(req.a) && !isNaN(req.b) ?
                ("<p class='result'>{a} * {b} = {result}</p>"
                .replace('{a}', req.a)
                .replace('{b}', req.b)
                .replace('{result}', req.a * req.b))
                : ""),
            "<p>Enter numbers to multiply</p>",
            "<form name='mult' action='/mult' method='get'>",
            "A: <input type='text' name='a' /><br>",
            "B: <input type='text' name='b' />",
            "<input type='submit' value='Submit' />",
            "</form>"
            ].join('\n'))
        );
}

Upvotes: 0

Views: 203

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

From what I can see it's not passing an array - it's joining it into a string before passing it in.

The reason for this is that string joining is (at least supposedly) more efficient than repeated concatenation, this of course depends on the implementation of join, but I'd imagine most implementations optimize it. JavaScript doesn't have a StringBuffer or a StringBuilder type, so joining an array is the closest thing you get.

Upvotes: 2

Related Questions