You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -2728,3 +2728,291 @@ By setting `hasName` equal to `name`, you set `hasName` equal to whatever value
2728
2728
2729
2729
</p>
2730
2730
</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
+
functionsum(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
+
exportdefault () =>"Hello world"
2793
+
exportconstname="Lydia"
2794
+
2795
+
// index.js
2796
+
import*asdatafrom"./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
+
classPerson {
2824
+
constructor(name) {
2825
+
this.name= name
2826
+
}
2827
+
}
2828
+
2829
+
constmember=newPerson("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
+
functionPerson() {
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
+
functiongiveLydiaPizza() {
2889
+
return"Here is pizza!"
2890
+
}
2891
+
2892
+
functiongiveLydiaChocolate() {
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
+
functiongiveLydiaPizza() {
2921
+
return"Here is pizza!"
2922
+
}
2923
+
2924
+
functiongiveLydiaChocolate() {
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
+
constperson= {
2953
+
name:"Lydia",
2954
+
age:21
2955
+
}
2956
+
2957
+
for (const [x, y] ofObject.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.
`...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.
0 commit comments