{{ message }}

Instantly share code, notes, and snippets.

# pirple-author/prereqs.md

Last active Oct 28, 2018
The Node.js Master Class - Prerequisites Test

# The Node.js Master Class - Prerequisites Test

### If you ace this 10-question quiz, you'll get 50% off your enrollment in the Node.js Master Class

#### Question 1

In the code snippet below, in which order will the statements log?

```console.log('a');
setTimeout(function(){
console.log('b');
},1000);
console.log('c');```

A) a b c

B) c b a

C) a b c b

D) a c b

#### Question 2

In the snippet below, what is the value of foo?

```var myArray = ['a','b','c'];
var foo = myArray[3];```

A) a

B) b

C) c

D) undefined

#### Question 3

In Javascript, why does this expression evaluate to false?

`0.2 + 0.4 == '0.6'`

A) You're comparing a string to a number. The types are different!

B) You should be using 3 equals signs

C) A ramification of floating point precision: 0.2 + 0.4 does not equal 0.6

D) I don't know

#### Question 4

In the code snippet below, what will be logged to the console?

```var foo = 3;
console.log(foo);
console.log(++foo);
console.log(foo++);```

A) 3 4 4

B) 3 3 5

C) 3 4 5

D) 3 5 5

#### Question 5

In the snippet below, what is the "type" of foo? In order words, if we use the "typeof" operator on foo, what will it return?

`var foo = [1,2,3];`

A) Object

B) Array

C) String

D) Buffer

#### Question 6

In javascript, is zero truthy or falsy? In order words, will this expression evaluate to true or false?

`0 ? true : false;`

A) truthy

B) falsy

C) None of the above

#### Question 7

In Javascript, what will be the value of foo?

```var myArray = [1,2,3];
var foo = myArray.indexOf(4);```

A) false

B) undefined

C) -1

D) 0

E) NaN

#### Question 8

In the terminal: if I have a file foo.txt and I write

`cp foo.txt newFoo.txt `

What will be the result?

A) foo.txt will be renamed to newFoo.txt

B) The contents of foo.txt will be copied to newFoo.txt, which will be created

C) foo.txt will be opened, nothing will happen to newFoo.txt

#### Question 9

In the terminal: if I have a file foo.txt and I write

`mv foo.txt newFoo.txt `

What will be the result?

A) foo.txt will be renamed to newFoo.txt

B) The contents of foo.txt will be copied to newFoo.txt, which will be created

C) foo.txt will be opened, nothing will happen to newFoo.txt

#### Question 10

In the terminal, which command lists all of the files in a directory, including hidden files?

A) la

B) ls

C) ls -a

D) ls -hidden

E) la -h

F) list all

Thanks to Javascript's asynchronous nature, "b" will actually be logged out last, well after "c". This is using a 'timer' function called setTimeout, which allows you to delay execution of pieces of code. setTimeout, and a few other timers, are available in the Node.js runtime as well.

Since arrays start counting at position zero (myArray[0]), the position referenced above is actually the 4th position in the array, not the 3rd. And as you can see, there is no 4th position defined, so it will return "undefined"

As with many languages, the Node.js runtime (and JS in general) uses floating-point arithmetic when doing math. So in the above example: 0.2 + 0.4 doesn't equal 0.6. It actually equals 0.6000000000000001. Therefore, the expression above evaluates to false. Since we're using 2 equals signs instead of 3, the "types" are not actually the problem.

In javascript foo++ and ++foo have different behaviors. As you can see, ++foo increments foo and then returns the value to you. Whereas foo++ returns the current value of foo to you, and then increments it later.

This question trips up almost everyone. If it caught you, don't worry.

The answer is "object" because Javascript considers Arrays to just be an object with some special properties. So in this example:

`var foo = [1,2,3];`

When you console log out the "type" of foo (using Javascript's "typeof" operator), it says "object"

```console.log(typeof(foo));

// "object"```

Knowledge of this quirk comes in handy when you're parsing payloads / parameters and trying to validate that they are what you think they are. In this case, if the typeof(foo) equals "object" it might actually be an array. There's a few other ways to check to see if it's an array. One of them is:

```console.log(foo instanceof Array)

// true```

In reality, Objects are really just associative arrays, and Arrays are a kind of Object. But what matters here is that Javascript considers an Array's "type" to be "object"

Although most numbers are truthy, zero is not. Keep this in mind if you're ever trying to use an "if" statement with a variable that might be zero.

When looking for the index of a certain value (within an array), Javascript will return -1 if it can't find the element you're looking for. This can be confusing; you'd be forgiven for thinking "undefined" would be the right answer here.