Skip to content

Commit d145b34

Browse files
authored
Merge pull request lydiahallie#362 from lydiahallie/new
New
2 parents bc89ddc + a74b233 commit d145b34

File tree

2 files changed

+200
-8
lines changed

2 files changed

+200
-8
lines changed

README.md

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
---
66

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>
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>
98

109
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>
1110

@@ -4718,7 +4717,7 @@ Objects aren't iterable by default. An iterable is an iterable if the iterator p
47184717
47194718
---
47204719
4721-
###### <a name=20200607></a>146. What's the output?
4720+
###### 146. What's the output?
47224721
47234722
```javascript
47244723
let count = 0;
@@ -4890,7 +4889,7 @@ At last, we invoke the `unshift` method on the `fruit` array, which modifies the
48904889
48914890
---
48924891
4893-
###### <a name=20200608></a>151. What's the output?
4892+
###### 151. What's the output?
48944893
48954894
```javascript
48964895
const animals = {};
@@ -4988,5 +4987,102 @@ runPromises()
49884987
49894988
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.
49904989
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+
49915087
</p>
49925088
</details>

en-EN/README.md

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
---
66

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>
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>
98

109
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>
1110

@@ -4718,7 +4717,7 @@ Objects aren't iterable by default. An iterable is an iterable if the iterator p
47184717
47194718
-----
47204719
4721-
###### <a name=20200607</a>146. What's the output?
4720+
###### 146. What's the output?
47224721
47234722
```javascript
47244723
let count = 0;
@@ -4890,7 +4889,7 @@ At last, we invoke the `unshift` method on the `fruit` array, which modifies the
48904889
48914890
---
48924891
4893-
###### <a name=20200608></a>151. What's the output?
4892+
###### 151. What's the output?
48944893
48954894
```javascript
48964895
const animals = {};
@@ -4988,5 +4987,102 @@ runPromises()
49884987
49894988
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.
49904989
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+
49915087
</p>
49925088
</details>

0 commit comments

Comments
 (0)