|
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=#20191224><b>December 24th</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=#20200608><b>June 8th</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 |
|
@@ -4886,3 +4886,106 @@ At last, we invoke the `unshift` method on the `fruit` array, which modifies the
|
4886 | 4886 |
|
4887 | 4887 | </p>
|
4888 | 4888 | </details>
|
| 4889 | +
|
| 4890 | +--- |
| 4891 | +
|
| 4892 | +###### <a name=20200608></a>151. What's the output? |
| 4893 | +
|
| 4894 | +```javascript |
| 4895 | +const animals = {}; |
| 4896 | +let dog = { emoji: '🐶' } |
| 4897 | +let cat = { emoji: '🐈' } |
| 4898 | + |
| 4899 | +animals[dog] = { ...dog, name: "Mara" } |
| 4900 | +animals[cat] = { ...cat, name: "Sara" } |
| 4901 | + |
| 4902 | +console.log(animals[dog]) |
| 4903 | +``` |
| 4904 | +
|
| 4905 | +- A: `{ emoji: "🐶", name: "Mara" }` |
| 4906 | +- B: `{ emoji: "🐈", name: "Sara" }` |
| 4907 | +- C: `undefined` |
| 4908 | +- D: `ReferenceError` |
| 4909 | +
|
| 4910 | +<details><summary><b>Answer</b></summary> |
| 4911 | +<p> |
| 4912 | +
|
| 4913 | +#### Answer: B |
| 4914 | +
|
| 4915 | +Object keys are converted to strings. |
| 4916 | +
|
| 4917 | +Since the value of `dog` is an object, `animals[dog]` actually means that we’re creating a new property called `"object Object"` equal to the new object. `animals["object Object"]` is now equal to `{ emoji: "🐶", name: "Mara"}`. |
| 4918 | +
|
| 4919 | +`cat` is also an object, which means that `animals[cat]` actually means that we’re overwriting the value of `animals[``"``object Object``"``]` with the new cat properties. |
| 4920 | +
|
| 4921 | +Logging `animals[dog]`, or actually `animals["object Object"]` since converting the `dog` object to a string results `"object Object"`, returns the `{ emoji: "🐈", name: "Sara" }`. |
| 4922 | +
|
| 4923 | +</p> |
| 4924 | +</details> |
| 4925 | +
|
| 4926 | +--- |
| 4927 | +
|
| 4928 | +###### 152. What's the output? |
| 4929 | +
|
| 4930 | +```javascript |
| 4931 | +const user = { |
| 4932 | + |
| 4933 | + updateEmail: email => { |
| 4934 | + this.email = email |
| 4935 | + } |
| 4936 | +} |
| 4937 | + |
| 4938 | +user. updateEmail( "[email protected]") |
| 4939 | +console.log(user.email) |
| 4940 | +``` |
| 4941 | +
|
| 4942 | +- A: `my@email.com` |
| 4943 | +- B: `new@email.com` |
| 4944 | +- C: `undefined` |
| 4945 | +- D: `ReferenceError` |
| 4946 | +
|
| 4947 | +<details><summary><b>Answer</b></summary> |
| 4948 | +<p> |
| 4949 | +
|
| 4950 | +#### Answer: A |
| 4951 | +
|
| 4952 | +The `updateEmail` function is an arrow function, and is not bound to the `user` object. This means that the `this` keyword is not referring to the `user` object, but refers to the global scope in this case. The value of `email` within the `user` object does not get updated. When logging the value of `user.email`, the original value of `my@email.com` gets returned. |
| 4953 | +
|
| 4954 | +</p> |
| 4955 | +</details> |
| 4956 | +
|
| 4957 | +--- |
| 4958 | +
|
| 4959 | +###### 153. What's the output? |
| 4960 | +
|
| 4961 | +```javascript |
| 4962 | +const promise1 = Promise.resolve('First') |
| 4963 | +const promise2 = Promise.resolve('Second') |
| 4964 | +const promise3 = Promise.reject('Third') |
| 4965 | +const promise4 = Promise.resolve('Fourth') |
| 4966 | + |
| 4967 | +const runPromises = async () => { |
| 4968 | + const res1 = await Promise.all([promise1, promise2]) |
| 4969 | + const res2 = await Promise.all([promise3, promise4]) |
| 4970 | + return [res1, res2] |
| 4971 | +} |
| 4972 | + |
| 4973 | +runPromises() |
| 4974 | + .then(res => console.log(res)) |
| 4975 | + .catch(err => console.log(err)) |
| 4976 | +``` |
| 4977 | +
|
| 4978 | +- A: `[['First', 'Second'], ['Fourth']]` |
| 4979 | +- B: `[['First', 'Second'], ['Third', 'Fourth']]` |
| 4980 | +- C: `[['First', 'Second']]` |
| 4981 | +- D: `'Third'` |
| 4982 | +
|
| 4983 | +<details><summary><b>Answer</b></summary> |
| 4984 | +<p> |
| 4985 | +
|
| 4986 | +#### Answer: D |
| 4987 | +
|
| 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 | +
|
| 4990 | +</p> |
| 4991 | +</details> |
0 commit comments