Skip to content

Latest commit

 

History

History
1094 lines (690 loc) · 13.7 KB

book1-chap2-quiz.md

File metadata and controls

1094 lines (690 loc) · 13.7 KB

Quiz - YDKJS: Up & Going 2/3

Chapter 2: Into JavaScript

Self-Evaluation

Section: Values & Types


1. Name the 7 built-in types available in JavaScript.

your answer here

2. Name the two ways to access object properties.

your answer here

3. Typeof. Look at the following snippets and write what would be the ouput.
Snippet #1
console.log(typeof a);
var a;

your answer here

Snippet #2
console.log(typeof "hello world");

your answer here

Snippet #3
console.log(typeof false);

your answer here

Snippet #4
console.log(typeof 21);

your answer here

Snippet #5
console.log(typeof null);

your answer here

Snippet #6
console.log(typeof undefined);

your answer here

Snippet #7
console.log(typeof { name: "John" });

your answer here

Snippet #8
console.log(typeof typeof 42);

your answer here

Snippet #9
console.log(typeof 3.1416);

your answer here

Snippet #10
console.log(typeof "10");

your answer here

Snippet #11
var person = {
  name: "Peter",
  age: 40
};

console.log(typeof person.name);
console.log(typeof person.age);
console.log(typeof person.lastName);

your answer here

Snippet #12
console.log(typeof []);

your answer here

Snippet #13
console.log(typeof ["Hello", 20, true][2]);

your answer here

Snippet #14
console.log(typeof ["Hello", 20, true][3]);

your answer here

Snippet #15
console.log(typeof { a: 2 }["a"]);

your answer here

Snippet #16
var index = "c";
console.log({ a: 1, b: 2, c: 3 }[index]);

your answer here

Snippet #17
console.log({ x: 100, y: 200 }.x);

your answer here

Snippet #18
function getX (point) {
  return point.x;
}

getX.PI = '3.1416';

console.log(typeof getX);
console.log(typeof getX({ x: 34, y: 54 }));
console.log(typeof getX.PI);

your answer here

Snippet #19
var n = "47";
console.log(typeof n);
console.log(typeof Number(n));

your answer here

Snippet #20
var n = "47";
console.log(typeof (n * 2));

your answer here

4. Coercion. Label the following code snippets with explicit or implicit accordingly.
Snippet #21
var a = "13";
var b = a * 2;
console.log(a); // "13"
console.log(b); // 26

your answer here

Snippet #22
var a = "13";
var b = Number(a);
console.log(a); // "13"
console.log(b); // 13

your answer here

5. List the falsy values in JS.

your answer here

6. Boolean Coercion. Write the result true or false for the following snippets.
Snippet #23
Boolean('');

your answer here

Snippet #24
Boolean('.');

your answer here

Snippet #25
Boolean("");

your answer here

Snippet #26
Boolean(0);

your answer here

Snippet #26-2
Boolean("0");

your answer here

Snippet #27
Boolean(1);

your answer here

Snippet #28
Boolean(1/0);

your answer here

Snippet #29
Boolean(0/1);

your answer here

Snippet #30
Boolean(NaN);

your answer here

Snippet #31
Boolean(Infinity);

your answer here

Snippet #32
Boolean(0 + "0");

your answer here

Snippet #33
Boolean("0" + 0);

your answer here

Snippet #34
Boolean("0" * 1);

your answer here

Snippet #35
Boolean(1 * "0");

your answer here

Snippet #36
Boolean(-1 * 0);

your answer here

Snippet #37
Boolean(null);

your answer here

Snippet #38
Boolean(undefined);

your answer here

Snippet #39
Boolean(false);

your answer here

Snippet #40
Boolean(true);

your answer here

Snippet #41
Boolean([]);

your answer here

Snippet #42
Boolean([1, 2, 3]);

your answer here

Snippet #43
Boolean([0].toString());

your answer here

Snippet #43-2
Boolean([1, '', {}][1]);

your answer here

Snippet #43-3
Boolean([1, '', { n: 0 }][2].n);

your answer here

Snippet #44
Boolean([].toString());

your answer here

Snippet #45
Boolean({});

your answer here

Snippet #45-2
Boolean({}.toString());

your answer here

Snippet #46
Boolean({ name: "Doe" });

your answer here

Snippet #47
Boolean({ toString: function () {
  return '';
}}.toString());

your answer here

Snippet #48
Boolean({ number: 0 }.number);

your answer here

Snippet #49
Boolean({ char: 'a' }.char);

your answer here

Snippet #50
Boolean(function noop() {});

your answer here

7. Operator that checks for value equality with coercion allowed:

your answer here

8. Operator that checks for value equality without allowing coercion:

your answer here

9. Operator that checks for value non-equality with coercion allowed:

your answer here

10. Operator that checks for value non-equality without allowing coercion:

your answer here

11. Equality Coercion. Write the result true or false for the following snippets:
Snippet #51
"12" == 12;

your answer here

Snippet #51-2
12 === "12";

your answer here

Snippet #52
1 == true;

your answer here

Snippet #52-2
true === 1;

your answer here

Snippet #53
false == 0;

your answer here

Snippet #54
false == "false";

your answer here

Snippet #55
"" == false;

your answer here

Snippet #56
null == false;

your answer here

Snippet #57
undefined == null;

your answer here

Snippet #58
false == undefined;

your answer here

Snippet #59
0 == "";

your answer here

Snippet #60
0 == null;

your answer here

Snippet #61
var a;
a == null;

your answer here

Snippet #62
({} == {});

your answer here

Snippet #63
({} === {});

your answer here

Snippet #64
 [] == [];

your answer here

Snippet #65
 [1, 2, 3] === [1, 2, 3];

your answer here

Snippet #66
 [1, 2, 3] == "1,2,3";

your answer here

Snippet #67
 NaN == NaN;

your answer here

Snippet #68
 NaN === NaN;

your answer here

Snippet #68-2
 (function noop (){}) == (function noop (){});

your answer here

Snippet #68-3
 (function noop (){}).toString() == (function noop (){});

your answer here

12. Inequality Coercion. Write true or false for the following snippets
Snippet #69
 2 > "1";

your answer here

Snippet #70
 "a" < "b";

your answer here

Snippet #71
 3 < "a";

your answer here

Snippet #72
 "3" < "a";

your answer here

Snippet #73
 0 > NaN;

your answer here

Variables


13. Are these valid JS indentifiers? Fill the table with true or false
identifier is valid?
Name your answer here
0duck your answer here
last.name your answer here
$account your answer here
_age your answer here
-price your answer here
car[123] your answer here
for your answer here
14. What's hoisting?

your answer here

15. Hoisting. What would be the output for this snippet?
Snippet #74
a = 10;

foo ();

function foo () {
  a = 5;

  console.log(a);

  var a;
}

var a;

console.log(a);

your answer here

16. What's the main difference between var and let?

your answer here

17. Nested Scopes, var & let. Write the output for the following code snippets
Snippet #75
var a = 1;

function foo () {
  if (a == 1) {
    var b = 2;
  }

  console.log(b);
}

foo();

your answer here

Snippet #76
var a = 1;

function foo () {
  if (a == 1) {
    let b = 2;
  }

  console.log(b);
}

foo();

your answer here

Snippet #77
function foo () {
  var a = 10
}

foo();

console.log(a);

your answer here

Snippet #78
function foo () {
  a = 20
}

foo();

console.log(a);

your answer here

Section: Conditionals


18. Write the code to log if a number is even or odd using the if statement, switch statement and the conditional operator ?: aka ternay operator

Clue: you can determine if a number is even if the remainder of n divided by 2 is equal to 0. Use the remainder operator %.

if Solution:

// your code here

switch Solution:

// your code here

ternary Solution:

// your code here

Section: Strict Mode


19. In your own words, what's use strict;?

your answer here

20. Use Strict. Write the output for the following code snippets
Snippet #79
function yummy () {
  a = 50
}

yummy();

console.log(a);

your answer here

Snippet #80
'use strict';

function yummy () {
  a = 50
}

yummy();

console.log(a);

your answer here

Section: Functions As Values


21. Complete the sentence

Functions are the primary mechanism of _____ in JS.

your answer here

22. Create a function square that takes one parameter number that returns the result of that number multiplied by itself. You need to perform this in 3 different ways
22.1. by using function declaration syntax.

Solution:

// your code here
22.2. by using function expression with an anonymous function syntax.

Solution:

// your code here
22.3 by using function expression with a named function syntax.

Solution:

// your code here
23. What's an IIFE? Give an example.

your answer here

// your code here
24. What's a closure in JS?

your answer here

25. Closure. What would be the output for the following code?
Snippet #81
function createHello (greeting) {
  return function greet (name) {
    return greeting + " " + name + "!";
  }
}

var greetInSpanish = createHello('Hola');

console.log(greetInSpanish("Pedro"));
console.log(createHello('Hi')("Mike"));

your answer here

Section: this Keyword


26. this. Write the output for the following code
Snippet #82
// Kitties
function sayHello() {
  console.log("Hello " + this.name);
}

var name = "Fluffy";

sayHello();

var kitty = {
  name: "Buttercup",
  sayHello: sayHello
};

kitty.sayHello();

sayHello.call({ name: "Cocoa" });

new sayHello();

your answer here

Section: Prototypes


27. Prototypes. Write the output for the following code
Snippet #83
var animal = {
  walk: "I'm walking!"
};

var bird = Object.create(animal);

bird.fly = "I'm flying!";

var penguin = Object.create(bird);

penguin.fly = "I can't fly :(";
penguin.swim = "I'm swimming!";

var canary = Object.create(bird);

var tiger = Object.create(animal);

console.log(tiger.walk);
console.log(tiger.fly);
console.log(canary.fly);
console.log(canary.walk);
console.log(canary.swim);
console.log(penguin.fly);
console.log(penguin.walk);
console.log(penguin.swim);

your answer here

Section: Old & New


28. What are the two main techniques to make older browsers work with newer features available in JS? Describe each

your answer here

Section: Non-JavaScript


29. Is the alert method provided by the JS engine?

your answer here

Section: Challenges


2.1 Create a module in an IIFE stored in a variable calculator that exposes 4 methods: plus, minus, times and dividedBy. Each method should receive one parameter firstNumber and return a function that receives another parameter secondNumber. Every method should perform the operation it describes, for example, it's expected that plus adds the firstNumber to the secondNumber, dividedBy should divide the secondNumber by the firstNumber and so on. The idea is to have "stored" the firstNumber to perform the next operations with it.

TIP: You can create a script.jsfile and test your code in the browser or node.js.

Solution:

// create your calculator module here
var calculator;

// Do NOT touch this code
const testCases = [
  { type: "plus", n1: 3, n2: [1, 2, 3], expected: [4, 5, 6] },
  { type: "minus", n1: 1, n2: [1, 0, -1], expected: [0, -1, -2] },
  { type: "times", n1: 5, n2: [2, 5, 10], expected: [10, 25, 50] },
  { type: "dividedBy", n1: 10, n2: [10, 100, 1000], expected: [1, 10, 100] }
];

const result = testCases.every(test => {
  const operation = calculator[test.type](test.n1);
  return test.n2.every((number, i) => {
    const testPassed = operation(number) === test.expected[i];
    if (!testPassed) {
      console.log(`Expected: ${operation(number)} to be: ${test.expected[i]}. Operation: secondNumber(${number}) ${test.type} firstNumber(${test.n1})`);
    }
    return testPassed;
  });
});

console.log("All tests passed: ", result);