Skip to content

Commit 1c036f4

Browse files
committed
Add questions 146-150
1 parent 38098f7 commit 1c036f4

File tree

2 files changed

+342
-0
lines changed

2 files changed

+342
-0
lines changed

README.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4745,3 +4745,174 @@ Objects aren't iterable by default. An iterable is an iterable if the iterator p
47454745
</p>
47464746
</details>
47474747
4748+
---
4749+
4750+
###### 146. What's the output?
4751+
4752+
```javascript
4753+
let count = 0;
4754+
const nums = [0, 1, 2, 3];
4755+
4756+
nums.forEach(num => {
4757+
if (num) count += 1
4758+
})
4759+
4760+
console.log(count)
4761+
```
4762+
4763+
- A: 1
4764+
- B: 2
4765+
- C: 3
4766+
- D: 4
4767+
4768+
<details><summary><b>Answer</b></summary>
4769+
<p>
4770+
4771+
#### Answer: C
4772+
4773+
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`.
4774+
4775+
</p>
4776+
</details>
4777+
4778+
---
4779+
4780+
###### 147. What's the output?
4781+
4782+
```javascript
4783+
function getFruit(fruits) {
4784+
console.log(fruits?.[1]?.[1])
4785+
}
4786+
4787+
getFruit([['🍊', '🍌'], ['🍍']])
4788+
getFruit()
4789+
getFruit([['🍍'], ['🍊', '🍌']])
4790+
```
4791+
4792+
- A: `null`, `undefined`, 🍌
4793+
- B: `[]`, `null`, 🍌
4794+
- C: `[]`, `[]`, 🍌
4795+
- D: `undefined`, `undefined`, 🍌
4796+
4797+
<details><summary><b>Answer</b></summary>
4798+
<p>
4799+
4800+
#### Answer: D
4801+
4802+
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`.
4803+
4804+
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`.
4805+
4806+
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.
4807+
4808+
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.
4809+
4810+
</p>
4811+
</details>
4812+
4813+
---
4814+
4815+
###### 148. What's the output?
4816+
4817+
```javascript
4818+
class Calc {
4819+
constructor() {
4820+
this.count = 0
4821+
}
4822+
4823+
increase() {
4824+
this.count ++
4825+
}
4826+
}
4827+
4828+
const calc = new Calc()
4829+
new Calc().increase()
4830+
4831+
console.log(calc.count)
4832+
```
4833+
4834+
- A: `0`
4835+
- B: `1`
4836+
- C: `undefined`
4837+
- D: `ReferenceError`
4838+
4839+
<details><summary><b>Answer</b></summary>
4840+
<p>
4841+
4842+
#### Answer: A
4843+
4844+
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`.
4845+
4846+
</p>
4847+
</details>
4848+
4849+
---
4850+
4851+
###### 149. What's the output?
4852+
4853+
```javascript
4854+
const user = {
4855+
4856+
password: "12345"
4857+
}
4858+
4859+
const updateUser = ({ email, password }) => {
4860+
if (email) {
4861+
Object.assign(user, { email })
4862+
}
4863+
4864+
if (password) {
4865+
user.password = password
4866+
}
4867+
4868+
return user
4869+
}
4870+
4871+
const updatedUser = updateUser({ email: "[email protected]" })
4872+
4873+
console.log(updatedUser === user)
4874+
```
4875+
4876+
- A: `false`
4877+
- B: `true`
4878+
- C: `TypeError`
4879+
- D: `ReferenceError`
4880+
4881+
<details><summary><b>Answer</b></summary>
4882+
<p>
4883+
4884+
#### Answer: B
4885+
4886+
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`.
4887+
4888+
</p>
4889+
</details>
4890+
4891+
---
4892+
4893+
###### 150. What's the output?
4894+
4895+
```javascript
4896+
const fruit = ['🍌', '🍊', '🍎']
4897+
4898+
fruit.slice(0, 1)
4899+
fruit.splice(0, 1)
4900+
fruit.unshift('🍇')
4901+
```
4902+
4903+
- A: `['🍌', '🍊', '🍎']`
4904+
- B: `['🍊', '🍎']`
4905+
- C: `['🍇', '🍊', '🍎']`
4906+
- D: `['🍇', '🍌', '🍊', '🍎']`
4907+
4908+
<details><summary><b>Answer</b></summary>
4909+
<p>
4910+
4911+
#### Answer: C
4912+
4913+
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.
4914+
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 `['🍊', '🍎']`.
4915+
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 `['🍇', '🍊', '🍎']`.
4916+
4917+
</p>
4918+
</details>

en-EN/README.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4745,3 +4745,174 @@ Objects aren't iterable by default. An iterable is an iterable if the iterator p
47454745
</p>
47464746
</details>
47474747
4748+
-----
4749+
4750+
###### 146. What's the output?
4751+
4752+
```javascript
4753+
let count = 0;
4754+
const nums = [0, 1, 2, 3];
4755+
4756+
nums.forEach(num => {
4757+
if (num) count += 1
4758+
})
4759+
4760+
console.log(count)
4761+
```
4762+
4763+
- A: 1
4764+
- B: 2
4765+
- C: 3
4766+
- D: 4
4767+
4768+
<details><summary><b>Answer</b></summary>
4769+
<p>
4770+
4771+
#### Answer: C
4772+
4773+
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`.
4774+
4775+
</p>
4776+
</details>
4777+
4778+
---
4779+
4780+
###### 147. What's the output?
4781+
4782+
```javascript
4783+
function getFruit(fruits) {
4784+
console.log(fruits?.[1]?.[1])
4785+
}
4786+
4787+
getFruit([['🍊', '🍌'], ['🍍']])
4788+
getFruit()
4789+
getFruit([['🍍'], ['🍊', '🍌']])
4790+
```
4791+
4792+
- A: `null`, `undefined`, 🍌
4793+
- B: `[]`, `null`, 🍌
4794+
- C: `[]`, `[]`, 🍌
4795+
- D: `undefined`, `undefined`, 🍌
4796+
4797+
<details><summary><b>Answer</b></summary>
4798+
<p>
4799+
4800+
#### Answer: D
4801+
4802+
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`.
4803+
4804+
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`.
4805+
4806+
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.
4807+
4808+
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.
4809+
4810+
</p>
4811+
</details>
4812+
4813+
---
4814+
4815+
###### 148. What's the output?
4816+
4817+
```javascript
4818+
class Calc {
4819+
constructor() {
4820+
this.count = 0
4821+
}
4822+
4823+
increase() {
4824+
this.count ++
4825+
}
4826+
}
4827+
4828+
const calc = new Calc()
4829+
new Calc().increase()
4830+
4831+
console.log(calc.count)
4832+
```
4833+
4834+
- A: `0`
4835+
- B: `1`
4836+
- C: `undefined`
4837+
- D: `ReferenceError`
4838+
4839+
<details><summary><b>Answer</b></summary>
4840+
<p>
4841+
4842+
#### Answer: A
4843+
4844+
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`.
4845+
4846+
</p>
4847+
</details>
4848+
4849+
---
4850+
4851+
###### 149. What's the output?
4852+
4853+
```javascript
4854+
const user = {
4855+
4856+
password: "12345"
4857+
}
4858+
4859+
const updateUser = ({ email, password }) => {
4860+
if (email) {
4861+
Object.assign(user, { email })
4862+
}
4863+
4864+
if (password) {
4865+
user.password = password
4866+
}
4867+
4868+
return user
4869+
}
4870+
4871+
const updatedUser = updateUser({ email: "[email protected]" })
4872+
4873+
console.log(updatedUser === user)
4874+
```
4875+
4876+
- A: `false`
4877+
- B: `true`
4878+
- C: `TypeError`
4879+
- D: `ReferenceError`
4880+
4881+
<details><summary><b>Answer</b></summary>
4882+
<p>
4883+
4884+
#### Answer: B
4885+
4886+
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`.
4887+
4888+
</p>
4889+
</details>
4890+
4891+
---
4892+
4893+
###### 150. What's the output?
4894+
4895+
```javascript
4896+
const fruit = ['🍌', '🍊', '🍎']
4897+
4898+
fruit.slice(0, 1)
4899+
fruit.splice(0, 1)
4900+
fruit.unshift('🍇')
4901+
```
4902+
4903+
- A: `['🍌', '🍊', '🍎']`
4904+
- B: `['🍊', '🍎']`
4905+
- C: `['🍇', '🍊', '🍎']`
4906+
- D: `['🍇', '🍌', '🍊', '🍎']`
4907+
4908+
<details><summary><b>Answer</b></summary>
4909+
<p>
4910+
4911+
#### Answer: C
4912+
4913+
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.
4914+
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 `['🍊', '🍎']`.
4915+
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 `['🍇', '🍊', '🍎']`.
4916+
4917+
</p>
4918+
</details>

0 commit comments

Comments
 (0)