Skip to content

Commit 39e0f07

Browse files
authored
Merge pull request lydiahallie#195 from lydiahallie/new
Questions 87-95
2 parents 90e27f1 + 4c243e9 commit 39e0f07

File tree

16 files changed

+3321
-15
lines changed

16 files changed

+3321
-15
lines changed

README.md

Lines changed: 303 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ Want to get an email whenever I've added more questions? <br />
1111

1212

1313
List of available languages:
14-
* [English](./README.md)
15-
* [العربية](./README_AR.md)
16-
* [اللغة العامية - Egyptian Arabic](./README_ar-EG.md)
17-
* [Bosanski](./README-bs_BS.md)
18-
* [Deutsch](./README-de_DE.md)
19-
* [Español](./README-ES.md)
20-
* [Français](./README_fr-FR.md)
21-
* [日本語](./README-ja_JA.md)
22-
* [한국어](./README-ko_KR.md)
23-
* [Português Brasil](./README_pt_BR.md)
24-
* [Русский](./README_ru-RU.md)
25-
* [Українська мова](./README-ua_UA.md)
26-
* [Tiếng Việt](./README-vi.md)
27-
* [中文版本](./README-zh_CN.md)
28-
* [Türkçe](./README-tr_TR.md)
14+
* [English](./en-EN/README.md)
15+
* [العربية](./ar-AR/README_AR.md)
16+
* [اللغة العامية - Egyptian Arabic](./ar-EG/README_ar-EG.md)
17+
* [Bosanski](./bs-BS/README-bs_BS.md)
18+
* [Deutsch](./de-DE/README-de_DE.md)
19+
* [Español](./es-ES/README-ES.md)
20+
* [Français](./fr-FR/README_fr-FR.md)
21+
* [日本語](./ja-JA/README-ja_JA.md)
22+
* [한국어](./ko-KR/README-ko_KR.md)
23+
* [Português Brasil](./pt-BR/README_pt_BR.md)
24+
* [Русский](./ru-RU/README_ru-RU.md)
25+
* [Українська мова](./ua-UA/README-ua_UA.md)
26+
* [Tiếng Việt](./vi-VI/README-vi.md)
27+
* [中文版本](./zh-CN/README-zh_CN.md)
28+
* [Türkçe](./tr-TR/README-tr_TR.md)
2929

3030

3131
---
@@ -2728,3 +2728,291 @@ By setting `hasName` equal to `name`, you set `hasName` equal to whatever value
27282728

27292729
</p>
27302730
</details>
2731+
2732+
---
2733+
2734+
###### 87. What's the output?
2735+
2736+
```javascript
2737+
console.log("I want pizza"[0])
2738+
```
2739+
2740+
- A: `"""`
2741+
- B: `"I"`
2742+
- C: `SyntaxError`
2743+
- D: `undefined`
2744+
2745+
<details><summary><b>Answer</b></summary>
2746+
<p>
2747+
2748+
#### Answer: B
2749+
2750+
In order to get an character on a specific index in a string, you can use bracket notation. The first character in the string has index 0, and so on. In this case we want to get the element which index is 0, the character `"I'`, which gets logged.
2751+
2752+
Note that this method is not supported in IE7 and below. In that case, use `.charAt()`
2753+
2754+
</p>
2755+
</details>
2756+
2757+
---
2758+
2759+
###### 88. What's the output?
2760+
2761+
```javascript
2762+
function sum(num1, num2 = num1) {
2763+
console.log(num1 + num2)
2764+
}
2765+
2766+
sum(10)
2767+
```
2768+
2769+
- A: `NaN`
2770+
- B: `20`
2771+
- C: `ReferenceError`
2772+
- D: `undefined`
2773+
2774+
<details><summary><b>Answer</b></summary>
2775+
<p>
2776+
2777+
#### Answer: B
2778+
2779+
You can set a default parameter's value equal to another parameter of the function, as long as they've been defined _before_ the default parameter. We pass the value `10` to the `sum` function. If the `sum` function only receives 1 argument, it means that the value for `num2` is not passed, amd the value of `num1` is equal to the passed value `10` in this case. The default value of `num2` is the value of `num1`, which is `10`. `num1 + num2` returns `20`.
2780+
2781+
If you're trying to set a default parameter's value equal to a parameter which is defined _after_ (to the right), the parameter's value hasn't been initialized yet, which will throw an error.
2782+
2783+
</p>
2784+
</details>
2785+
2786+
---
2787+
2788+
###### 89. What's the output?
2789+
2790+
```javascript
2791+
// module.js
2792+
export default () => "Hello world"
2793+
export const name = "Lydia"
2794+
2795+
// index.js
2796+
import * as data from "./module"
2797+
2798+
console.log(data)
2799+
```
2800+
2801+
- A: `{ default: function default(), name: "Lydia" }`
2802+
- B: `{ default: function default() }`
2803+
- C: `{ default: "Hello world", name: "Lydia" }`
2804+
- D: Global object of `module.js`
2805+
2806+
<details><summary><b>Answer</b></summary>
2807+
<p>
2808+
2809+
#### Answer: A
2810+
2811+
With the `import * as name` syntax, we import _all exports_ from the `module.js` file into the `index.js` file as a new object called `data` is created. In the `module.js` file, there are two exports: the default export, and a named export. The default export is a function which returns the string `"Hello World"`, and the named export is a variable called `name` which has the value of the string `"Lydia"`.
2812+
2813+
The `data` object has a `default` property for the default export, other properties have the names of the named exports and their corresponding values.
2814+
2815+
</p>
2816+
</details>
2817+
2818+
---
2819+
2820+
###### 90. What's the output?
2821+
2822+
```javascript
2823+
class Person {
2824+
constructor(name) {
2825+
this.name = name
2826+
}
2827+
}
2828+
2829+
const member = new Person("John")
2830+
console.log(typeof member)
2831+
```
2832+
2833+
- A: `"class"`
2834+
- B: `"function"`
2835+
- C: `"object"`
2836+
- D: `"string"`
2837+
2838+
<details><summary><b>Answer</b></summary>
2839+
<p>
2840+
2841+
#### Answer: B
2842+
2843+
Classes are syntactical sugar for function constructors. The equivalent of the `Person` class as a function constructor would be:
2844+
2845+
```javascript
2846+
function Person() {
2847+
this.name = name
2848+
}
2849+
```
2850+
2851+
Which results in the same value. A class is a function under the hood, the `typeof` keyword returns `"function"` for functions. `typeof Person` returns `"function"`.
2852+
2853+
</p>
2854+
</details>
2855+
2856+
---
2857+
2858+
###### 91. What's the output?
2859+
2860+
```javascript
2861+
let newList = [1, 2, 3].push(4)
2862+
2863+
console.log(newList.push(5))
2864+
```
2865+
2866+
- A: `[1, 2, 3, 4, 5]`
2867+
- B: `[1, 2, 3, 5]`
2868+
- C: `[1, 2, 3, 4]`
2869+
- D: `Error`
2870+
2871+
<details><summary><b>Answer</b></summary>
2872+
<p>
2873+
2874+
#### Answer: D
2875+
2876+
The `.push` method returns the _new length_ of the array, not the array itself! By setting `newList` equal to `[1, 2, 3].push(4)`, we set `newList` equal to the new length of the array: `4`.
2877+
2878+
Then, we try to use the `.push` method on `newList`. Since `newList` is the numerical value `4`, we cannot use the `.push` method: a TypeError is thrown.
2879+
2880+
</p>
2881+
</details>
2882+
2883+
---
2884+
2885+
###### 92. What's the output?
2886+
2887+
```javascript
2888+
function giveLydiaPizza() {
2889+
return "Here is pizza!"
2890+
}
2891+
2892+
function giveLydiaChocolate() {
2893+
return "Here's chocolate... now go hit the gym already."
2894+
}
2895+
2896+
console.log(giveLydiaPizza.prototype)
2897+
console.log(giveLydiaChocolate.prototype)
2898+
```
2899+
2900+
- A: `{ constructor: ...}` `{ constructor: ...}`
2901+
- B: `{}` `{ constructor: ...}`
2902+
- C: `{ constructor: ...}` `{}`
2903+
- D: `{ constructor: ...}` `undefined`
2904+
2905+
<details><summary><b>Answer</b></summary>
2906+
<p>
2907+
2908+
#### Answer: D
2909+
2910+
Regular functions, such as the `giveLydiaPizza` function, have a `prototype` property, which is an object (prototype object) with a `constructor` property. Arrow functions however, such as the `giveLydiaChocolate` function, do not have this `prototype` property. `undefined` gets returned when trying to access the `prototype` property using `giveLydiaChocolate.prototype`.
2911+
2912+
</p>
2913+
</details>
2914+
2915+
---
2916+
2917+
###### 93. What's the output?
2918+
2919+
```javascript
2920+
function giveLydiaPizza() {
2921+
return "Here is pizza!"
2922+
}
2923+
2924+
function giveLydiaChocolate() {
2925+
return "Here's chocolate... now go hit the gym already."
2926+
}
2927+
2928+
console.log(giveLydiaPizza.prototype)
2929+
console.log(giveLydiaChocolate.prototype)
2930+
```
2931+
2932+
- A: `{ constructor: ...}` `{ constructor: ...}`
2933+
- B: `{}` `{ constructor: ...}`
2934+
- C: `{ constructor: ...}` `{}`
2935+
- D: `{ constructor: ...}` `undefined`
2936+
2937+
<details><summary><b>Answer</b></summary>
2938+
<p>
2939+
2940+
#### Answer: D
2941+
2942+
Regular functions, such as the `giveLydiaPizza` function, have a `prototype` property, which is an object (prototype object) with a `constructor` property. Arrow functions however, such as the `giveLydiaChocolate` function, do not have this `prototype` property. `undefined` gets returned when trying to access the `prototype` property using `giveLydiaChocolate.prototype`.
2943+
2944+
</p>
2945+
</details>
2946+
2947+
---
2948+
2949+
###### 94. What's the output?
2950+
2951+
```javascript
2952+
const person = {
2953+
name: "Lydia",
2954+
age: 21
2955+
}
2956+
2957+
for (const [x, y] of Object.entries(person)) {
2958+
console.log(x, y)
2959+
}
2960+
```
2961+
2962+
- A: `name` `Lydia` and `age` `21`
2963+
- B: `["name", "Lydia"]` and `["age", 21]`
2964+
- C: `["name", "age"]` and `undefined`
2965+
- D: `Error`
2966+
2967+
<details><summary><b>Answer</b></summary>
2968+
<p>
2969+
2970+
#### Answer: A
2971+
2972+
`Object.entries(person)` returns an array of nested arrays, containing the keys and objects:
2973+
2974+
`[ [ 'name', 'Lydia' ], [ 'age', 21 ] ]`
2975+
2976+
Using the `for-of` loop, we can iterate over each element in the array, the subarrays in this case. We can destructure the subarrays instantly in the for-of loop, using `const [x, y]`. `x` is equal to the first element in the subarray, `y` is equal to the second element in the subarray.
2977+
2978+
The first subarray is `[ "name", "Lydia" ]`, with `x` equal to `"name"`, and `y` equal to `"Lydia"`, which get logged.
2979+
The second subarray is `[ "age", 21 ]`, with `x` equal to `"age"`, and `y` equal to `21`, which get logged.
2980+
2981+
</p>
2982+
</details>
2983+
2984+
---
2985+
2986+
###### 95. What's the output?
2987+
2988+
```javascript
2989+
function getItems(fruitList, ...args, favoriteFruit) {
2990+
return [...fruitList, ...args, favoriteFruit]
2991+
}
2992+
2993+
getItems(["banana", "apple"], "pear", "orange")
2994+
```
2995+
2996+
- A: `["banana", "apple", "pear", "orange"]`
2997+
- B: `[["banana", "apple"], "pear", "orange"]`
2998+
- C: `["banana", "apple", ["pear"], "orange"]`
2999+
- D: `SyntaxError`
3000+
3001+
<details><summary><b>Answer</b></summary>
3002+
<p>
3003+
3004+
#### Answer: D
3005+
3006+
`...args` is a rest parameter. The rest parameter's value is an array containing all remaining arguments, **and can only be the last parameter**! In this example, the rest parameter was the second parameter. This is not possible, and will throw a syntax error.
3007+
3008+
```javascript
3009+
function getItems(fruitList, favoriteFruit, ...args) {
3010+
return [...fruitList, ...args, favoriteFruit]
3011+
}
3012+
3013+
getItems(["banana", "apple"], "pear", "orange")
3014+
```
3015+
3016+
The above example works. This returns the array `[ 'banana', 'apple', 'orange', 'pear' ]`
3017+
</p>
3018+
</details>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)