# Introduction to Node.js and JavaScript

> Date 26/08/2014

This course (TT00CC06-3001) is about getting to know [Node.js](http://nodejs.org/) 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](http://leftoversalad.tumblr.com/post/103503118002).

## 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.

```javascript
function doubleInteger(i) {
    return i;
}
```

```javascript
function doubleInteger(i) {
    return i * 2;
}
```

```javascript
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.

```javascript
function isNumberEven(i) {

}
```

```javascript
function isNumberEven(i) {
    return i % 2 === 0;
}
```

```javascript
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`.

```javascript
function getFileExtension(i) {

}
```

```javascript
function getFileExtension(i) {
    return i.indexOf('.') !== -1 ? i.split('.').pop() : false;
}
```

```javascript
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.

```javascript
function longestString(i) {

}
```

```javascript
function longestString(i) {
    return i.reduce(function (curr, prev) {
        if (typeof curr !== 'string' || prev && prev.length > curr.length) {
            return prev;
        }
        return curr;
    });
}
```

```javascript
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.

```javascript
function arraySum(i) {

}
```

```javascript
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;
}
```

```javascript
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?](http://perfectionkills.com/javascript-quiz-es6)

## 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.

* [Codeanywhere](https://codeanywhere.com)
* [Nitrous.IO](https://www.nitrous.io/join/jN91bVe8Boc?utm_source=nitrous.io\&utm_medium=copypaste\&utm_campaign=referral)

## Links related to the lecture subject

* [Eloquent JavaScript](http://eloquentjavascript.net/)
* [Javascript BEST PRACTICES PART 1](http://www.thinkful.com/learn/javascript-best-practices-1)
* [Short, in-depth explanations of JavaScript code and concepts](http://jsforallof.us/)
* [Programming JavaScript Applications](http://chimera.labs.oreilly.com/books/1234000000262/index.html)
* [Understanding Scope and Context in JavaScript](http://ryanmorr.com/understanding-scope-and-context-in-javascript/)
* [The Nodefirm - Training Courses](http://thenodefirm.com/blog/2013/08/25/new-public-training-courses/)
* [nodemeatspace.com](http://nodemeatspace.com/)
* [nodeschool.io](http://nodeschool.io/)
* \[An Absolute Beginner's Guide to Node.js]\(<http://blog.modulus.io/absolute-beginners-guide-to-nodejs> "An Absolute Beginner's Guide to Node.js")
* \[What's in a Function Name?]\(<http://bocoup.com/weblog/whats-in-a-function-name> "What's in a Function Name?")
* [Every Possible Way to Define a Javascript Function](http://www.bryanbraun.com/2014/11/27/every-possible-way-to-define-a-javascript-function)
* [Node.js tools for Visual Studio](http://nodejstools.codeplex.com/)
* [Free Visual Studio for Developers](http://www.visualstudio.com/products/free-developer-offers-vs)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://paazmaya.gitbook.io/modern-web-tools-with-node-js/lectures/01-introduction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
