Skip to content

Commit 5dd980b

Browse files
committed
Add questions 64-69
1 parent 027d831 commit 5dd980b

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed

README.md

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,3 +1960,210 @@ The unary operator `++` _first returns_ the value of the operand, _then incremen
19601960

19611961
</p>
19621962
</details>
1963+
1964+
---
1965+
1966+
###### 64. What's the output?
1967+
1968+
```javascript
1969+
const value = { number: 10 };
1970+
1971+
const multiply = (x = { ...value }) => {
1972+
console.log((x.number * 2));
1973+
};
1974+
1975+
multiply();
1976+
multiply();
1977+
multiply(value);
1978+
multiply(value);
1979+
```
1980+
1981+
- A: `20`, `40`, `80`, `160`
1982+
- B: `20`, `40`, `20`, `40`
1983+
- C: `20`, `20`, `20`, `40`
1984+
- D: `NaN`, `NaN`, `20`, `40`
1985+
1986+
<details><summary><b>Answer</b></summary>
1987+
<p>
1988+
1989+
#### Answer: C
1990+
1991+
In ES6, we can initialize parameters with a default value. The value of the parameter will be the default value, if no other value has been passed to the function, or if the value of the parameter is `"undefined"`. In this case, we spread the properties of the `value` object into a new object, so `x` has the default value of `{ number: 10 }`.
1992+
1993+
The defeault argument is evaluated at _call time_! Every time we call the function, a _new_ object is created. We invoke the `multiply` function the first two times without passing a value: `x` has the default value of `{ number: 10 }`. We then log the multiplied value of that number, which is `20`.
1994+
1995+
The third time we invoke multiply, we do pass an argument: the object valled `value`. The `*=` operator is actually shorthand for `x.number = x.number * 2`: we modify the value of `x.number`, and log the multiplied value `20`.
1996+
1997+
The fourth time, we pass the `value` object again. `x.number` was previously modified to `20`, so `x.number *= 2` logs `40`.
1998+
1999+
</p>
2000+
</details>
2001+
2002+
---
2003+
2004+
###### 65. What's the output?
2005+
2006+
```javascript
2007+
[1, 2, 3, 4].reduce((x, y) => console.log(x, y));
2008+
```
2009+
2010+
- A: `1` `2` and `3` `3` and `6` `4`
2011+
- B: `1` `2` and `2` `3` and `3` `4`
2012+
- C: `1` `undefined` and `2` `undefined` and `3` `undefined` and `4` `undefined`
2013+
- D: `1` `2` and `undefined` `3` and `undefined` `4`
2014+
2015+
<details><summary><b>Answer</b></summary>
2016+
<p>
2017+
2018+
#### Answer: D
2019+
2020+
The first argument that the `reduce` method receives is the _accumulator_, `x` in this case. The second argument is the _current value_, `y`. With the reduce method, we execute a callback function on every element in the array, which could ultimately result in one single value.
2021+
2022+
In this example, we are not returning any values, we are simply logging the values of the accumulator and the current value.
2023+
2024+
The value of the accumulator is equal to the previously returned value of the callback function. If you don't pass the optional `initialValue` argument to the `reduce` method, the accumulator is equal to the first element on the first call.
2025+
2026+
On the first call, the accumulator (`x`) is `1`, and the current value (`y`) is `2`. We don't return from the callback function, we log the accumulator and current value: `1` and `2` get logged.
2027+
2028+
If you don't return a value from a function, it returns `undefined`. On the next call, the accumulator is `undefined`, and the current value is `3`. `undefined` and `3` get logged.
2029+
2030+
On the fourth call, we again don't return from the callback function. The accumulator is again `undefined`, and the current value is `4`. `undefined` and `4` get logged.
2031+
</p>
2032+
</details>
2033+
2034+
---
2035+
2036+
###### 66. With which constructor can we successfully extend the `Dog` class?
2037+
2038+
```javascript
2039+
class Dog {
2040+
constructor(name) {
2041+
this.name = name;
2042+
}
2043+
};
2044+
2045+
class Labrador extends Dog {
2046+
// 1
2047+
constructor(name, size) {
2048+
this.size = size;
2049+
}
2050+
// 2
2051+
constructor(name, size) {
2052+
super(name);
2053+
this.size = size;
2054+
}
2055+
// 3
2056+
constructor(size) {
2057+
super(name);
2058+
this.size = size;
2059+
}
2060+
// 4
2061+
constructor(name, size) {
2062+
this.name = name;
2063+
this.size = size;
2064+
}
2065+
2066+
};
2067+
```
2068+
2069+
- A: 1
2070+
- B: 2
2071+
- C: 3
2072+
- D: 4
2073+
2074+
<details><summary><b>Answer</b></summary>
2075+
<p>
2076+
2077+
#### Answer: B
2078+
2079+
In a derived class, you cannot access the `this` keyword before calling `super`. If you try to do that, it will throw a ReferenceError: 1 and 4 would throw a reference error.
2080+
2081+
With the `super` keyword, we call that parent class's constructor with the given arguments. The parent's constructor receives the `name` argument, so we need to pass `name` to `super`.
2082+
2083+
The `Dog` class receives two arguments, `name` since it extends `Animal`, and `size` as an extra property on the `Dog` class. They both need to be passed to the constructor function on `Dog`, which is done correctly using constructor 2.
2084+
</p>
2085+
</details>
2086+
2087+
---
2088+
2089+
###### 67. With which constructor can we successfully extend the `Dog` class?
2090+
2091+
```javascript
2092+
// index.js
2093+
console.log('running index.js);
2094+
import { sum } from './sum.js';
2095+
console.log(sum(1, 2));
2096+
2097+
// sum.js
2098+
console.log('running sum.js');
2099+
export const sum = (a, b) => a + b;
2100+
```
2101+
2102+
- A: `running index.js`, `running sum.js`, `3`
2103+
- B: `running sum.js`, `running index.js`, `3`
2104+
- C: `running sum.js`, `3`, `running index.js`
2105+
- D: `running index.js`, `undefined`, `running sum.js`
2106+
2107+
<details><summary><b>Answer</b></summary>
2108+
<p>
2109+
2110+
#### Answer: B
2111+
2112+
With the `import` keyword, all imported modules are _pre-parsed_. This means that the imported modules get run _first_, the code in the file which imports the module gets executed _after_.
2113+
2114+
This is a difference between `require()` in CommonJS and `import`! With `require()`, you can load dependencies on demand while the code is being run. If we would have used `require` instead of `import`, `running index.js`, `running sum.js`, `3` would have been logged to the console.
2115+
2116+
</p>
2117+
</details>
2118+
2119+
---
2120+
2121+
###### 68. What's the output?
2122+
2123+
```javascript
2124+
console.log(Number(2) === Number(2))
2125+
console.log(Boolean(false) === Boolean(false))
2126+
console.log(Symbol('foo') === Symbol('foo'))
2127+
```
2128+
2129+
- A: `true`, `true`, `false`
2130+
- B: `false`, `true`, `false`
2131+
- C: `true`, `false`, `true`
2132+
- D: `true`, `true`, `true`
2133+
2134+
<details><summary><b>Answer</b></summary>
2135+
<p>
2136+
2137+
#### Answer: A
2138+
2139+
Every Symbol is entirely unique.The purpose of the argument passed to the Symbol, is to give the Symbol a description. The value of the Symbol is not dependent on the passed argument. As we test equality, we are creating two entirely new symbols: the first `Symbol('foo')`, and the second `Symbol('foo')`. These two values are unique and not equal to each other, `Symbol('foo') === Symbol('foo')` returns `false`.
2140+
2141+
</p>
2142+
</details>
2143+
2144+
---
2145+
2146+
###### 69. What's the output?
2147+
2148+
```javascript
2149+
const name = "Lydia Hallie"
2150+
console.log(name.padStart(13))
2151+
console.log(name.padStart(2))
2152+
```
2153+
2154+
- A: `"Lydia Hallie"`, `"Lydia Hallie"`
2155+
- B: `" Lydia Hallie"`, `" Lydia Hallie"`
2156+
- C: `" Lydia Hallie"`, `"Lydia Hallie"`
2157+
- D: `"Lydia Hallie"`, `"Lyd"`,
2158+
2159+
<details><summary><b>Answer</b></summary>
2160+
<p>
2161+
2162+
#### Answer: C
2163+
2164+
With the `padStart` method, we can add padding to the beginning of a string. The value passed to this method is the _total_ length of the string together with the padding. The string `"Lydia Hallie"` has a length of `12`. `name.padStart(13)` inserts 1 space at the start of the string, because 12 + 1 is 13.
2165+
2166+
If the argument passed to the `padStart` method is smaller than the length of the array, no padding will be added.
2167+
2168+
</p>
2169+
</details>

0 commit comments

Comments
 (0)