Skip to content

Commit 93e8396

Browse files
authored
Merge pull request lydiahallie#359 from lydiahallie/new
Add questions 146-150
2 parents 2102548 + 2c31e01 commit 93e8396

File tree

2 files changed

+344
-0
lines changed

2 files changed

+344
-0
lines changed

README.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4714,3 +4714,175 @@ Objects aren't iterable by default. An iterable is an iterable if the iterator p
47144714
47154715
</p>
47164716
</details>
4717+
4718+
---
4719+
4720+
###### 146. What's the output?
4721+
4722+
```javascript
4723+
let count = 0;
4724+
const nums = [0, 1, 2, 3];
4725+
4726+
nums.forEach(num => {
4727+
if (num) count += 1
4728+
})
4729+
4730+
console.log(count)
4731+
```
4732+
4733+
- A: 1
4734+
- B: 2
4735+
- C: 3
4736+
- D: 4
4737+
4738+
<details><summary><b>Answer</b></summary>
4739+
<p>
4740+
4741+
#### Answer: C
4742+
4743+
The `if` condition within the `forEach` loop checks whether the value of `num` is truthy or falsy. Since the first number in the `nums` array is `0`, a falsy value, the `if` statement's code block won't be executed. `count` only gets incremented for the other 3 numbers in the `nums` array, `1`, `2` and `3`. Since `count` gets incremented by `1` 3 times, the value of `count` is `3`.
4744+
4745+
</p>
4746+
</details>
4747+
4748+
---
4749+
4750+
###### 147. What's the output?
4751+
4752+
```javascript
4753+
function getFruit(fruits) {
4754+
console.log(fruits?.[1]?.[1])
4755+
}
4756+
4757+
getFruit([['🍊', '🍌'], ['🍍']])
4758+
getFruit()
4759+
getFruit([['🍍'], ['🍊', '🍌']])
4760+
```
4761+
4762+
- A: `null`, `undefined`, 🍌
4763+
- B: `[]`, `null`, 🍌
4764+
- C: `[]`, `[]`, 🍌
4765+
- D: `undefined`, `undefined`, 🍌
4766+
4767+
<details><summary><b>Answer</b></summary>
4768+
<p>
4769+
4770+
#### Answer: D
4771+
4772+
The `?` allows us to optionally access deeper nested properties within objects. We're trying to log the item on index `1` within the subarray that's on index `1` of the `fruits` array. If the subarray on index `1` in the `fruits` array doesn't exist, it'll simply return `undefined`. If the subarray on index `1` in the `fruits` array exists, but this subarray doesn't have an item on its `1` index, it'll also return `undefined`.
4773+
4774+
First, we're trying to log the second item in the `['🍍']` subarray of `[['🍊', '🍌'], ['🍍']]]`. This subarray only contains one item, which means there is no item on index `1`, and returns `undefined`.
4775+
4776+
Then, we're invoking the `getFruits` function without passing a value as an argument, which means that `fruits` has a value of `undefined` by default. Since we're conditionally chaining the item on index `1` of`fruits`, it returns `undefined` since this item on index `1` does not exist.
4777+
4778+
Lastly, we're trying to log the second item in the `['🍊', '🍌']` subarray of `['🍍'], ['🍊', '🍌']`. The item on index `1` within this subarray is `🍌`, which gets logged.
4779+
4780+
</p>
4781+
</details>
4782+
4783+
---
4784+
4785+
###### 148. What's the output?
4786+
4787+
```javascript
4788+
class Calc {
4789+
constructor() {
4790+
this.count = 0
4791+
}
4792+
4793+
increase() {
4794+
this.count ++
4795+
}
4796+
}
4797+
4798+
const calc = new Calc()
4799+
new Calc().increase()
4800+
4801+
console.log(calc.count)
4802+
```
4803+
4804+
- A: `0`
4805+
- B: `1`
4806+
- C: `undefined`
4807+
- D: `ReferenceError`
4808+
4809+
<details><summary><b>Answer</b></summary>
4810+
<p>
4811+
4812+
#### Answer: A
4813+
4814+
We set the variable `calc` equal to a new instance of the `Calc` class. Then, we instantiate a new instance of `Calc`, and invoke the `increase` method on this instance. Since the count property is within the constructor of the `Calc` class, the count property is not shared on the prototype of `Calc`. This means that the value of count has not been updated for the instance calc points to, count is still `0`.
4815+
4816+
</p>
4817+
</details>
4818+
4819+
---
4820+
4821+
###### 149. What's the output?
4822+
4823+
```javascript
4824+
const user = {
4825+
4826+
password: "12345"
4827+
}
4828+
4829+
const updateUser = ({ email, password }) => {
4830+
if (email) {
4831+
Object.assign(user, { email })
4832+
}
4833+
4834+
if (password) {
4835+
user.password = password
4836+
}
4837+
4838+
return user
4839+
}
4840+
4841+
const updatedUser = updateUser({ email: "[email protected]" })
4842+
4843+
console.log(updatedUser === user)
4844+
```
4845+
4846+
- A: `false`
4847+
- B: `true`
4848+
- C: `TypeError`
4849+
- D: `ReferenceError`
4850+
4851+
<details><summary><b>Answer</b></summary>
4852+
<p>
4853+
4854+
#### Answer: B
4855+
4856+
The `updateUser` function updates the values of the `email` and `password` properties on user, if their values are passed to the function, after which the function returns the `user` object. The returned value of the `updateUser` function is the `user` object, which means that the value of updatedUser is a reference to the same `user` object that `user` points to. `updatedUser === user` equals `true`.
4857+
4858+
</p>
4859+
</details>
4860+
4861+
---
4862+
4863+
###### 150. What's the output?
4864+
4865+
```javascript
4866+
const fruit = ['🍌', '🍊', '🍎']
4867+
4868+
fruit.slice(0, 1)
4869+
fruit.splice(0, 1)
4870+
fruit.unshift('🍇')
4871+
```
4872+
4873+
- A: `['🍌', '🍊', '🍎']`
4874+
- B: `['🍊', '🍎']`
4875+
- C: `['🍇', '🍊', '🍎']`
4876+
- D: `['🍇', '🍌', '🍊', '🍎']`
4877+
4878+
<details><summary><b>Answer</b></summary>
4879+
<p>
4880+
4881+
#### Answer: C
4882+
4883+
First, we invoke the `slice` method on the fruit array. The slice method does not modify the original array, but returns the value that it sliced off the array: the banana emoji.
4884+
Then, we invoke the `splice` method on the fruit array. The splice method does modify the original array, which means that the fruit array now consists of `['🍊', '🍎']`.
4885+
At last, we invoke the `unshift` method on the `fruit` array, which modifies the original array by adding the provided value, ‘🍇’ in this case, as the first element in the array. The fruit array now consists of `['🍇', '🍊', '🍎']`.
4886+
4887+
</p>
4888+
</details>

en-EN/README.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4714,3 +4714,175 @@ Objects aren't iterable by default. An iterable is an iterable if the iterator p
47144714
47154715
</p>
47164716
</details>
4717+
4718+
-----
4719+
4720+
###### 146. What's the output?
4721+
4722+
```javascript
4723+
let count = 0;
4724+
const nums = [0, 1, 2, 3];
4725+
4726+
nums.forEach(num => {
4727+
if (num) count += 1
4728+
})
4729+
4730+
console.log(count)
4731+
```
4732+
4733+
- A: 1
4734+
- B: 2
4735+
- C: 3
4736+
- D: 4
4737+
4738+
<details><summary><b>Answer</b></summary>
4739+
<p>
4740+
4741+
#### Answer: C
4742+
4743+
The `if` condition within the `forEach` loop checks whether the value of `num` is truthy or falsy. Since the first number in the `nums` array is `0`, a falsy value, the `if` statement's code block won't be executed. `count` only gets incremented for the other 3 numbers in the `nums` array, `1`, `2` and `3`. Since `count` gets incremented by `1` 3 times, the value of `count` is `3`.
4744+
4745+
</p>
4746+
</details>
4747+
4748+
---
4749+
4750+
###### 147. What's the output?
4751+
4752+
```javascript
4753+
function getFruit(fruits) {
4754+
console.log(fruits?.[1]?.[1])
4755+
}
4756+
4757+
getFruit([['🍊', '🍌'], ['🍍']])
4758+
getFruit()
4759+
getFruit([['🍍'], ['🍊', '🍌']])
4760+
```
4761+
4762+
- A: `null`, `undefined`, 🍌
4763+
- B: `[]`, `null`, 🍌
4764+
- C: `[]`, `[]`, 🍌
4765+
- D: `undefined`, `undefined`, 🍌
4766+
4767+
<details><summary><b>Answer</b></summary>
4768+
<p>
4769+
4770+
#### Answer: D
4771+
4772+
The `?` allows us to optionally access deeper nested properties within objects. We're trying to log the item on index `1` within the subarray that's on index `1` of the `fruits` array. If the subarray on index `1` in the `fruits` array doesn't exist, it'll simply return `undefined`. If the subarray on index `1` in the `fruits` array exists, but this subarray doesn't have an item on its `1` index, it'll also return `undefined`.
4773+
4774+
First, we're trying to log the second item in the `['🍍']` subarray of `[['🍊', '🍌'], ['🍍']]]`. This subarray only contains one item, which means there is no item on index `1`, and returns `undefined`.
4775+
4776+
Then, we're invoking the `getFruits` function without passing a value as an argument, which means that `fruits` has a value of `undefined` by default. Since we're conditionally chaining the item on index `1` of`fruits`, it returns `undefined` since this item on index `1` does not exist.
4777+
4778+
Lastly, we're trying to log the second item in the `['🍊', '🍌']` subarray of `['🍍'], ['🍊', '🍌']`. The item on index `1` within this subarray is `🍌`, which gets logged.
4779+
4780+
</p>
4781+
</details>
4782+
4783+
---
4784+
4785+
###### 148. What's the output?
4786+
4787+
```javascript
4788+
class Calc {
4789+
constructor() {
4790+
this.count = 0
4791+
}
4792+
4793+
increase() {
4794+
this.count ++
4795+
}
4796+
}
4797+
4798+
const calc = new Calc()
4799+
new Calc().increase()
4800+
4801+
console.log(calc.count)
4802+
```
4803+
4804+
- A: `0`
4805+
- B: `1`
4806+
- C: `undefined`
4807+
- D: `ReferenceError`
4808+
4809+
<details><summary><b>Answer</b></summary>
4810+
<p>
4811+
4812+
#### Answer: A
4813+
4814+
We set the variable `calc` equal to a new instance of the `Calc` class. Then, we instantiate a new instance of `Calc`, and invoke the `increase` method on this instance. Since the count property is within the constructor of the `Calc` class, the count property is not shared on the prototype of `Calc`. This means that the value of count has not been updated for the instance calc points to, count is still `0`.
4815+
4816+
</p>
4817+
</details>
4818+
4819+
---
4820+
4821+
###### 149. What's the output?
4822+
4823+
```javascript
4824+
const user = {
4825+
4826+
password: "12345"
4827+
}
4828+
4829+
const updateUser = ({ email, password }) => {
4830+
if (email) {
4831+
Object.assign(user, { email })
4832+
}
4833+
4834+
if (password) {
4835+
user.password = password
4836+
}
4837+
4838+
return user
4839+
}
4840+
4841+
const updatedUser = updateUser({ email: "[email protected]" })
4842+
4843+
console.log(updatedUser === user)
4844+
```
4845+
4846+
- A: `false`
4847+
- B: `true`
4848+
- C: `TypeError`
4849+
- D: `ReferenceError`
4850+
4851+
<details><summary><b>Answer</b></summary>
4852+
<p>
4853+
4854+
#### Answer: B
4855+
4856+
The `updateUser` function updates the values of the `email` and `password` properties on user, if their values are passed to the function, after which the function returns the `user` object. The returned value of the `updateUser` function is the `user` object, which means that the value of updatedUser is a reference to the same `user` object that `user` points to. `updatedUser === user` equals `true`.
4857+
4858+
</p>
4859+
</details>
4860+
4861+
---
4862+
4863+
###### 150. What's the output?
4864+
4865+
```javascript
4866+
const fruit = ['🍌', '🍊', '🍎']
4867+
4868+
fruit.slice(0, 1)
4869+
fruit.splice(0, 1)
4870+
fruit.unshift('🍇')
4871+
```
4872+
4873+
- A: `['🍌', '🍊', '🍎']`
4874+
- B: `['🍊', '🍎']`
4875+
- C: `['🍇', '🍊', '🍎']`
4876+
- D: `['🍇', '🍌', '🍊', '🍎']`
4877+
4878+
<details><summary><b>Answer</b></summary>
4879+
<p>
4880+
4881+
#### Answer: C
4882+
4883+
First, we invoke the `slice` method on the fruit array. The slice method does not modify the original array, but returns the value that it sliced off the array: the banana emoji.
4884+
Then, we invoke the `splice` method on the fruit array. The splice method does modify the original array, which means that the fruit array now consists of `['🍊', '🍎']`.
4885+
At last, we invoke the `unshift` method on the `fruit` array, which modifies the original array by adding the provided value, ‘🍇’ in this case, as the first element in the array. The fruit array now consists of `['🍇', '🍊', '🍎']`.
4886+
4887+
</p>
4888+
</details>

0 commit comments

Comments
 (0)