|
4 | 4 |
|
5 | 5 | ---
|
6 | 6 |
|
7 |
| -<span>I post multiple choice JavaScript questions on my [Instagram](https://www.instagram.com/theavocoder) **stories**, which I'll also post here! Last updated: <a href=#20200608><b>June 8th</b></a> |
| 7 | +<span>I post multiple choice JavaScript questions on my [Instagram](https://www.instagram.com/theavocoder) **stories**, which I'll also post here! Last updated: <a href=#20200612><b>June 12th</b></a> |
8 | 8 |
|
9 | 9 | From basic to advanced: test how well you know JavaScript, refresh your knowledge a bit, or prepare for your coding interview! :muscle: :rocket: I update this repo regularly with new questions. I added the answers in the **collapsed sections** below the questions, simply click on them to expand it. It's just for fun, good luck! :heart:</span>
|
10 | 10 |
|
@@ -4217,7 +4217,7 @@ With the `flat` method, we can create a new, flattened array. The depth of the f
|
4217 | 4217 |
|
4218 | 4218 | ---
|
4219 | 4219 |
|
4220 |
| -###### <a name=20191224></a>132. What's the output? |
| 4220 | +###### 132. What's the output? |
4221 | 4221 |
|
4222 | 4222 | ```javascript
|
4223 | 4223 | class Counter {
|
@@ -4889,7 +4889,7 @@ At last, we invoke the `unshift` method on the `fruit` array, which modifies the
|
4889 | 4889 |
|
4890 | 4890 | ---
|
4891 | 4891 |
|
4892 |
| -###### <a name=20200608></a>151. What's the output? |
| 4892 | +###### 151. What's the output? |
4893 | 4893 |
|
4894 | 4894 | ```javascript
|
4895 | 4895 | const animals = {};
|
@@ -4987,5 +4987,102 @@ runPromises()
|
4987 | 4987 |
|
4988 | 4988 | The `Promise.all` method runs the passed promises in parallel. If one promise fails, the `Promise.all` method _rejects) with the value of the rejected promise. In this case, `promise3` rejected with the value `"Third"`. We’re catching the rejected value in the chained `catch` method on the `runPromises` invocation to catch any errors within the `runPromises` function. Only `"Third"` gets logged, since `promise3` rejected with this value.
|
4989 | 4989 |
|
| 4990 | +</p> |
| 4991 | +</details> |
| 4992 | +
|
| 4993 | +--- |
| 4994 | +
|
| 4995 | +###### <a name=20200612></a>154. What should the value of `method` be to log `{ name: "Lydia", age: 22 }`? |
| 4996 | +
|
| 4997 | +```javascript |
| 4998 | +const keys = ["name", "age"] |
| 4999 | +const values = ["Lydia", 22] |
| 5000 | + |
| 5001 | +const method = /* ?? */ |
| 5002 | +Object[method](keys.map((_, i) => { |
| 5003 | + return [keys[i], values[i]] |
| 5004 | +})) // { name: "Lydia", age: 22 } |
| 5005 | +``` |
| 5006 | +
|
| 5007 | +- A: `entries` |
| 5008 | +- B: `values` |
| 5009 | +- C: `fromEntries` |
| 5010 | +- D: `forEach` |
| 5011 | +
|
| 5012 | +<details><summary><b>Answer</b></summary> |
| 5013 | +<p> |
| 5014 | +
|
| 5015 | +#### Answer: C |
| 5016 | +
|
| 5017 | +The `fromEntries` method turns a 2d array into an object. The first element in each subarray will be the key, and the second element in each subarray will be the value. In this case, we’re mapping over the `keys` array, which returns an array which first element is the item on the key array on the current index, and the second element is the item of the values array on the current index. |
| 5018 | +
|
| 5019 | +This creates an array of subarrays containing the correct keys and values, which results in `{ name: "Lydia", age: 22 }` |
| 5020 | +
|
| 5021 | +</p> |
| 5022 | +</details> |
| 5023 | +
|
| 5024 | +--- |
| 5025 | +
|
| 5026 | +###### 155. What's the output? |
| 5027 | +
|
| 5028 | +```javascript |
| 5029 | +const createMember = ({ email, address = {}}) => { |
| 5030 | + const validEmail = /.+\@.+\..+/.test(email) |
| 5031 | + if (!validEmail) throw new Error("Valid email pls") |
| 5032 | + |
| 5033 | + return { |
| 5034 | + email, |
| 5035 | + address: address ? address : null |
| 5036 | + } |
| 5037 | +} |
| 5038 | + |
| 5039 | +const member = createMember({ email : "[email protected]" }) |
| 5040 | +console.log(member) |
| 5041 | +``` |
| 5042 | +
|
| 5043 | +- A: `{ email : "[email protected]", address : null } ` |
| 5044 | +- B: `{ email : "[email protected]" } ` |
| 5045 | +- C: `{ email : "[email protected]", address : {} } ` |
| 5046 | +- D: `{ email : "[email protected]", address : undefined } ` |
| 5047 | +
|
| 5048 | +<details><summary><b>Answer</b></summary> |
| 5049 | +<p> |
| 5050 | +
|
| 5051 | +#### Answer: C |
| 5052 | +
|
| 5053 | +The default value of `address` is an empty object `{}`. When we set the variable `member` equal to the object returned by the `createMember` function, we didn't pass a value for address, which means that the value of address is the default empty object `{}`. An empty object is a truthy value, which means that the condition of the `address ? address : null` conditional returns `true`. The value of address is the empty object `{}`. |
| 5054 | +
|
| 5055 | +</p> |
| 5056 | +</details> |
| 5057 | +
|
| 5058 | +--- |
| 5059 | +
|
| 5060 | +###### 156. What's the output? |
| 5061 | +
|
| 5062 | +```javascript |
| 5063 | +let randomValue = { name: "Lydia" } |
| 5064 | +randomValue = 23 |
| 5065 | + |
| 5066 | +if (!typeof randomValue === "string") { |
| 5067 | + console.log("It's not a string!") |
| 5068 | +} else { |
| 5069 | + console.log("Yay it's a string!") |
| 5070 | +} |
| 5071 | +``` |
| 5072 | +
|
| 5073 | +- A: `It's not a string!` |
| 5074 | +- B: `Yay it's a string!` |
| 5075 | +- C: `TypeError` |
| 5076 | +- D: `undefined` |
| 5077 | +
|
| 5078 | +<details><summary><b>Answer</b></summary> |
| 5079 | +<p> |
| 5080 | +
|
| 5081 | +#### Answer: B |
| 5082 | +
|
| 5083 | +The condition within the `if` statement checks whether the value of `!typeof randomValue` is equal to `"string"`. The `!` operator converts the value to a boolean value. If the value is truthy, the returned value will be `false`, if the value is falsy, the returned value will be `true`. In this case, the returned value of `typeof randomValue` is the truthy value `"string"`, meaning that the value of `!typeof randomValue` is the boolean value `false`. |
| 5084 | +
|
| 5085 | +`!typeof randomValue === "string"` always returns false, since we're actually checking `false === "string"`. Since the condition returned `false`, the code block of the `else` statement gets run, and `Yay it's a string!` gets logged. |
| 5086 | +
|
4990 | 5087 | </p>
|
4991 | 5088 | </details>
|
0 commit comments