You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+207Lines changed: 207 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1960,3 +1960,210 @@ The unary operator `++` _first returns_ the value of the operand, _then incremen
1960
1960
1961
1961
</p>
1962
1962
</details>
1963
+
1964
+
---
1965
+
1966
+
###### 64. What's the output?
1967
+
1968
+
```javascript
1969
+
constvalue= { number:10 };
1970
+
1971
+
constmultiply= (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
+
classDog {
2040
+
constructor(name) {
2041
+
this.name= name;
2042
+
}
2043
+
};
2044
+
2045
+
classLabradorextendsDog {
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?
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 newsymbols: 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.
0 commit comments