Skip to content

Commit bc89ddc

Browse files
authored
Merge pull request lydiahallie#360 from lydiahallie/new
Add questions 151 - 153
2 parents fce2d16 + d18669b commit bc89ddc

File tree

2 files changed

+210
-2
lines changed

2 files changed

+210
-2
lines changed

README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
---
66

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=#20200607><b>June 7th</b></a>
7+
8+
<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>
89

910
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>
1011

@@ -4886,3 +4887,106 @@ At last, we invoke the `unshift` method on the `fruit` array, which modifies the
48864887
48874888
</p>
48884889
</details>
4890+
4891+
---
4892+
4893+
###### <a name=20200608></a>151. What's the output?
4894+
4895+
```javascript
4896+
const animals = {};
4897+
let dog = { emoji: '🐶' }
4898+
let cat = { emoji: '🐈' }
4899+
4900+
animals[dog] = { ...dog, name: "Mara" }
4901+
animals[cat] = { ...cat, name: "Sara" }
4902+
4903+
console.log(animals[dog])
4904+
```
4905+
4906+
- A: `{ emoji: "🐶", name: "Mara" }`
4907+
- B: `{ emoji: "🐈", name: "Sara" }`
4908+
- C: `undefined`
4909+
- D: `ReferenceError`
4910+
4911+
<details><summary><b>Answer</b></summary>
4912+
<p>
4913+
4914+
#### Answer: B
4915+
4916+
Object keys are converted to strings.
4917+
4918+
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"}`.
4919+
4920+
`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.
4921+
4922+
Logging `animals[dog]`, or actually `animals["object Object"]` since converting the `dog` object to a string results `"object Object"`, returns the `{ emoji: "🐈", name: "Sara" }`.
4923+
4924+
</p>
4925+
</details>
4926+
4927+
---
4928+
4929+
###### 152. What's the output?
4930+
4931+
```javascript
4932+
const user = {
4933+
4934+
updateEmail: email => {
4935+
this.email = email
4936+
}
4937+
}
4938+
4939+
user.updateEmail("[email protected]")
4940+
console.log(user.email)
4941+
```
4942+
4943+
- A: `my@email.com`
4944+
- B: `new@email.com`
4945+
- C: `undefined`
4946+
- D: `ReferenceError`
4947+
4948+
<details><summary><b>Answer</b></summary>
4949+
<p>
4950+
4951+
#### Answer: A
4952+
4953+
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.
4954+
4955+
</p>
4956+
</details>
4957+
4958+
---
4959+
4960+
###### 153. What's the output?
4961+
4962+
```javascript
4963+
const promise1 = Promise.resolve('First')
4964+
const promise2 = Promise.resolve('Second')
4965+
const promise3 = Promise.reject('Third')
4966+
const promise4 = Promise.resolve('Fourth')
4967+
4968+
const runPromises = async () => {
4969+
const res1 = await Promise.all([promise1, promise2])
4970+
const res2 = await Promise.all([promise3, promise4])
4971+
return [res1, res2]
4972+
}
4973+
4974+
runPromises()
4975+
.then(res => console.log(res))
4976+
.catch(err => console.log(err))
4977+
```
4978+
4979+
- A: `[['First', 'Second'], ['Fourth']]`
4980+
- B: `[['First', 'Second'], ['Third', 'Fourth']]`
4981+
- C: `[['First', 'Second']]`
4982+
- D: `'Third'`
4983+
4984+
<details><summary><b>Answer</b></summary>
4985+
<p>
4986+
4987+
#### Answer: D
4988+
4989+
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.
4990+
4991+
</p>
4992+
</details>

en-EN/README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
---
66

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=#20200607><b>June 7th</b></a>
7+
8+
<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>
89

910
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>
1011

@@ -4886,3 +4887,106 @@ At last, we invoke the `unshift` method on the `fruit` array, which modifies the
48864887
48874888
</p>
48884889
</details>
4890+
4891+
---
4892+
4893+
###### <a name=20200608></a>151. What's the output?
4894+
4895+
```javascript
4896+
const animals = {};
4897+
let dog = { emoji: '🐶' }
4898+
let cat = { emoji: '🐈' }
4899+
4900+
animals[dog] = { ...dog, name: "Mara" }
4901+
animals[cat] = { ...cat, name: "Sara" }
4902+
4903+
console.log(animals[dog])
4904+
```
4905+
4906+
- A: `{ emoji: "🐶", name: "Mara" }`
4907+
- B: `{ emoji: "🐈", name: "Sara" }`
4908+
- C: `undefined`
4909+
- D: `ReferenceError`
4910+
4911+
<details><summary><b>Answer</b></summary>
4912+
<p>
4913+
4914+
#### Answer: B
4915+
4916+
Object keys are converted to strings.
4917+
4918+
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"}`.
4919+
4920+
`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.
4921+
4922+
Logging `animals[dog]`, or actually `animals["object Object"]` since converting the `dog` object to a string results `"object Object"`, returns the `{ emoji: "🐈", name: "Sara" }`.
4923+
4924+
</p>
4925+
</details>
4926+
4927+
---
4928+
4929+
###### 152. What's the output?
4930+
4931+
```javascript
4932+
const user = {
4933+
4934+
updateEmail: email => {
4935+
this.email = email
4936+
}
4937+
}
4938+
4939+
user.updateEmail("[email protected]")
4940+
console.log(user.email)
4941+
```
4942+
4943+
- A: `my@email.com`
4944+
- B: `new@email.com`
4945+
- C: `undefined`
4946+
- D: `ReferenceError`
4947+
4948+
<details><summary><b>Answer</b></summary>
4949+
<p>
4950+
4951+
#### Answer: A
4952+
4953+
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.
4954+
4955+
</p>
4956+
</details>
4957+
4958+
---
4959+
4960+
###### 153. What's the output?
4961+
4962+
```javascript
4963+
const promise1 = Promise.resolve('First')
4964+
const promise2 = Promise.resolve('Second')
4965+
const promise3 = Promise.reject('Third')
4966+
const promise4 = Promise.resolve('Fourth')
4967+
4968+
const runPromises = async () => {
4969+
const res1 = await Promise.all([promise1, promise2])
4970+
const res2 = await Promise.all([promise3, promise4])
4971+
return [res1, res2]
4972+
}
4973+
4974+
runPromises()
4975+
.then(res => console.log(res))
4976+
.catch(err => console.log(err))
4977+
```
4978+
4979+
- A: `[['First', 'Second'], ['Fourth']]`
4980+
- B: `[['First', 'Second'], ['Third', 'Fourth']]`
4981+
- C: `[['First', 'Second']]`
4982+
- D: `'Third'`
4983+
4984+
<details><summary><b>Answer</b></summary>
4985+
<p>
4986+
4987+
#### Answer: D
4988+
4989+
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.
4990+
4991+
</p>
4992+
</details>

0 commit comments

Comments
 (0)