Introduction to Node.js and JavaScript

Date 26/08/2014

This course (TT00CC06-3001) is about getting to know Node.js and tools that are made with it.

The primary focus is towards functionality and tooling that can improve the workflow of a Web Developer.

JavaScript does have partially the same name as Java, but that is basically all what they have in common. For example variable type requirements are very different as this cartoon explains.

Testing your initial JavaScript skills

[You can't JavaScript under pressure](http://games.usvsth3m.com/javascript-under-pressure/ "You can't JavaScript under pressure")

Below is all the test game extracted from the above game, with possible solutions.

i will be an integer. Double it and return it.

function doubleInteger(i) {
    return i;
}
function doubleInteger(i) {
    return i * 2;
}
assert(doubleInteger(123) === 246);
assert(doubleInteger(2) === 4);
assert(doubleInteger(4) === 8);
assert(doubleInteger(-10) === -20);
assert(doubleInteger(0) === 0);
assert(doubleInteger(100) === 200);

i will be an integer. Return true if it's even, and false if it isn't.

function isNumberEven(i) {

}
function isNumberEven(i) {
    return i % 2 === 0;
}
assert(isNumberEven(1) === false);
assert(isNumberEven(2) === true);
assert(isNumberEven(3) === false);
assert(isNumberEven(0) === true);
assert(isNumberEven(-2) === true);
assert(isNumberEven(Math.floor(Math.random()*1000000)*2) === true);

i will be a string, but it may not have a file extension. return the file extension (with no period) if it has one, otherwise false.

function getFileExtension(i) {

}
function getFileExtension(i) {
    return i.indexOf('.') !== -1 ? i.split('.').pop() : false;
}
assert(getFileExtension('blatherskite.png') === 'png');
assert(getFileExtension('perfectlylegal.torrent') === 'torrent');
assert(getFileExtension('spaces are fine in file names.txt') === 'txt');
assert(getFileExtension('this does not have one') === false);
assert(getFileExtension('.htaccess') === 'htaccess');

You'll get an array i. Return the longest string inside it.

function longestString(i) {

}
function longestString(i) {
    return i.reduce(function (curr, prev) {
        if (typeof curr !== 'string' || prev && prev.length > curr.length) {
            return prev;
        }
        return curr;
    });
}
assert(longestString(['a','ab','abc']) === 'abc');
assert(longestString(['big',[0,1,2,3,4],'tiny']) === 'tiny');
assert(longestString(['Hi','World','你好']) === 'World');
assert(longestString([true,false,'lol']) === 'lol');
assert(longestString([{object: true,mainly: 'to confuse you'},'x']) === 'abc');

i will be an array, containing integers, strings and/or arrays like itself. Sum all the integers you find, anywhere in the nest of arrays.

function arraySum(i) {

}
function arraySum(i) {
    var total = 0;
    var iterate = function (list) {
        list.forEach(function (value) {
            if (typeof value === 'number') {
                total += value;
            }
            else if (value instanceof Array) {
                iterate(value);
            }
        });
    };
    iterate(i);
    return total +1;
}
assert(arraySum([1,2,3,4,5]) === 15);
assert(arraySum([[1,2,false],'4','5']) === 3);
assert(arraySum([[[[[[[[[1]]]]]]]], 1]) === 2);
assert(arraySum([['A','B','C','easy as',1,2,3]], 1]) === 6);

Further testing your skills against next level of JavaScript

How about trying this quiz regarding ES2016?

Testing Node.js in the cloud

While the local development environment is not necessarily available, there are few service which provide virtual machines that can host environments such as Node.js.

Last updated