Skip to content

Commit f1bf6d4

Browse files
committed
Add questions 55 to 63
1 parent 0fb6faf commit f1bf6d4

File tree

1 file changed

+299
-0
lines changed

1 file changed

+299
-0
lines changed

README.md

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,3 +1655,302 @@ However, we created a global variable `y` when setting `y` equal to `10`. This v
16551655

16561656
</p>
16571657
</details>
1658+
1659+
---
1660+
1661+
###### 55. What's the output?
1662+
1663+
```javascript
1664+
class Dog {
1665+
constructor(name) {
1666+
this.name = name;
1667+
}
1668+
}
1669+
1670+
Dog.prototype.bark = function() {
1671+
console.log(`Woof I am ${this.name}`);
1672+
};
1673+
1674+
const pet = new Dog("Mara");
1675+
1676+
pet.bark();
1677+
1678+
delete Dog.prototype.bark;
1679+
1680+
pet.bark();
1681+
```
1682+
1683+
- A: `"Woof I am Mara"`, `TypeError`
1684+
- B: `"Woof I am Mara"`,`"Woof I am Mara"`
1685+
- C: `"Woof I am Mara"`, `undefined`
1686+
- D: `TypeError`, `TypeError`
1687+
1688+
<details><summary><b>Answer</b></summary>
1689+
<p>
1690+
1691+
#### Answer: A
1692+
1693+
We can delete properties from objects using the `delete` keyword, also on the prototype. By deleting a property on the prototype, it is not available anymore in the prototype chain. In this case, the `bark` function is not available anymore on the prototype after `delete Dog.prototype.bark`, yet we still try to access it.
1694+
1695+
When we try to invoke something that is not a function, a `TypeError` is thrown. In this case `TypeError: pet.bark is not a function`, since `pet.bark` is `undefined`.
1696+
1697+
</p>
1698+
</details>
1699+
1700+
---
1701+
1702+
###### 56. What's the output?
1703+
1704+
```javascript
1705+
const set = new Set([1, 1, 2, 3, 4]);
1706+
1707+
console.log(set);
1708+
```
1709+
1710+
- A: `[1, 1, 2, 3, 4]`
1711+
- B: `[1, 2, 3, 4]`
1712+
- C: `{1, 1, 2, 3, 4}`
1713+
- D: `{1, 2, 3, 4}`
1714+
1715+
<details><summary><b>Answer</b></summary>
1716+
<p>
1717+
1718+
#### Answer: D
1719+
1720+
The `Set` object is a collection of _unique_ values: a value can only occur once in a set.
1721+
1722+
We passed the iterable `[1, 1, 2, 3, 4]` with a duplicate value `1`. Since we cannot have two of the same values in a set, one of them is removed. This results in `{1, 2, 3, 4}`.
1723+
1724+
</p>
1725+
</details>
1726+
1727+
---
1728+
1729+
###### 57. What's the output?
1730+
1731+
```javascript
1732+
// counter.js
1733+
let counter = 10;
1734+
export default counter;
1735+
```
1736+
1737+
```javascript
1738+
// index.js
1739+
import myCounter from "./counter";
1740+
1741+
myCounter += 1;
1742+
1743+
console.log(myCounter);
1744+
```
1745+
1746+
- A: `10`
1747+
- B: `11`
1748+
- C: `Error`
1749+
- D: `NaN`
1750+
1751+
<details><summary><b>Answer</b></summary>
1752+
<p>
1753+
1754+
#### Answer: C
1755+
1756+
An imported module is _read-only_: you cannot modify the imported module. Only the module that exports them can change its value.
1757+
1758+
When we try to increment the value of `myCounter`, it throws an error: `myCounter` is read-only and cannot be modified.
1759+
1760+
</p>
1761+
</details>
1762+
1763+
---
1764+
1765+
###### 58. What's the output?
1766+
1767+
```javascript
1768+
const name = "Lydia";
1769+
age = 21;
1770+
1771+
console.log(delete name);
1772+
console.log(delete age);
1773+
```
1774+
1775+
- A: `false`, `true`
1776+
- B: `"Lydia"`, `21`
1777+
- C: `true`, `true`
1778+
- D: `undefined`, `undefined`
1779+
1780+
<details><summary><b>Answer</b></summary>
1781+
<p>
1782+
1783+
#### Answer: A
1784+
1785+
The `delete` operator returns a boolena value: `true` on a successful deletion, else it'll return `false`. However, variables declared with the `var`, `const` or `let` keyword cannot be deleted using the `delete` operator.
1786+
1787+
The `name` variable was declared with a `const` keyword, so its deletion is not successful: `false` is returned. When we set `age` equal to `21`, we actually added a property called `age` to the global object. You can successfully delete properties from objects this way, also the global object, so `delete age` returns `true`.
1788+
1789+
</p>
1790+
</details>
1791+
1792+
---
1793+
1794+
###### 59. What's the output?
1795+
1796+
```javascript
1797+
const numbers = [1, 2, 3, 4, 5];
1798+
const [y] = numberes;
1799+
1800+
console.log(y);
1801+
```
1802+
1803+
- A: `[[1, 2, 3, 4, 5]]`
1804+
- B: `[1, 2, 3, 4, 5]`
1805+
- C: `1`
1806+
- D: `[1]`
1807+
1808+
<details><summary><b>Answer</b></summary>
1809+
<p>
1810+
1811+
#### Answer: C
1812+
1813+
We can unpack values from arrays or properties from objects through destructing. For example:
1814+
1815+
```javascript
1816+
[a, b] = [1, 2];
1817+
```
1818+
1819+
<img src="https://i.imgur.com/ADFpVop.png" width="200">
1820+
1821+
The value of `a` is now `1`, and the value of `b` is now `2`. What we actually did in the question, is:
1822+
1823+
```javascript
1824+
[y] = [1, 2, 3, 4, 5];
1825+
```
1826+
1827+
<img src="https://i.imgur.com/NzGkMNk.png" width="200">
1828+
1829+
This means that the value of `y` is equal to the first value in the array, which is the number `1`. When we log `y`, `1` is returned.
1830+
1831+
</p>
1832+
</details>
1833+
1834+
---
1835+
1836+
###### 60. What's the output?
1837+
1838+
```javascript
1839+
const user = { name: "Lydia", age: 21 };
1840+
const admin = { admin: true, ...user };
1841+
1842+
console.log(admin);
1843+
```
1844+
1845+
- A: `{ admin: true, user: { name: "Lydia", age: 21 } }`
1846+
- B: `{ admin: true, name: "Lydia", age: 21 }`
1847+
- C: `{ admin: true, user: ["Lydia", 21] }`
1848+
- D: `{ admin: true }`
1849+
1850+
<details><summary><b>Answer</b></summary>
1851+
<p>
1852+
1853+
#### Answer: B
1854+
1855+
It's possible to combine objects using the spread operator `...`. It lets you create copies of the key/value pairs of one object, and add them to another object. In this case, we create copies of the `user` object, and add them to the `admin` object. The `admin` object now contains the copied key/value pairs, which results in `{ admin: true, name: "Lydia", age: 21 }`.
1856+
1857+
</p>
1858+
</details>
1859+
1860+
---
1861+
1862+
###### 61. What's the output?
1863+
1864+
```javascript
1865+
const person = { name: "Lydia" };
1866+
1867+
Object.defineProperty(person, "age", { value: 21 });
1868+
1869+
console.log(person);
1870+
console.log(Object.keys(person));
1871+
```
1872+
1873+
- A: `{ name: "Lydia", age: 21 }`, `["name", "age"]`
1874+
- B: `{ name: "Lydia", age: 21 }`, `["name"]`
1875+
- C: `{ name: "Lydia"}`, `["name", "age"]`
1876+
- D: `{ name: "Lydia"}`, `["age"]`
1877+
1878+
<details><summary><b>Answer</b></summary>
1879+
<p>
1880+
1881+
#### Answer: B
1882+
1883+
With the `defineProperty` method, we can add new properties to an object, or modify existing ones. When we add a property to an object using the `defineProperty` method, they are by default _not enumerable_. The `Object.keys` method returns all _enumerable_ property names from an object, in this case only `"name"`.
1884+
1885+
Properties added using the `defineProperty` method are immutable by default. You can override this behavior using the `writable`, `configurable` and `enumerable` properties. This way, the `defineProperty` method gives you a lot more control over the properties you're adding to an object.
1886+
1887+
</p>
1888+
</details>
1889+
1890+
---
1891+
1892+
###### 62. What's the output?
1893+
1894+
```javascript
1895+
const settings = {
1896+
username: "lydiahallie",
1897+
level: 19,
1898+
health: 90
1899+
};
1900+
1901+
const data = JSON.stringify(settings, ["level", "health"]);
1902+
console.log(data);
1903+
```
1904+
1905+
- A: `"{"level":19, "health":90}"`
1906+
- B: `"{"username": "lydiahallie"}"`
1907+
- C: `"["level", "health"]"`
1908+
- D: `"{"username": "lydiahallie", "level":19, "health":90}"`
1909+
1910+
<details><summary><b>Answer</b></summary>
1911+
<p>
1912+
1913+
#### Answer: A
1914+
1915+
The second argument of `JSON.stringify` is the _replacer_. The replacer can either be a function or an array, and lets you control what and how the values should be stringified.
1916+
1917+
If the replacer is an _array_, only the properties which names are included in the array will be added to the JSON string. In this case, only the properies with the names `"level"` and `"health"` are included, `"username"` is excluded. `data` is now equal to `"{"level":19, "health":90}"`.
1918+
1919+
If the replacer is a _function_, this function gets called on every property in the object you're stringifying. The value returned from this function will be the value of the property when it's added to the JSON string. If the value is `undefined`, this property is excluded from the JSON string.
1920+
1921+
</p>
1922+
</details>
1923+
1924+
---
1925+
1926+
###### 63. What's the output?
1927+
1928+
```javascript
1929+
let num = 10;
1930+
1931+
const increaseNumber = () => num++;
1932+
const increasePassedNumber = number => number++;
1933+
1934+
const num1 = increaseNumber();
1935+
const num2 = increasePassedNumber(num1);
1936+
1937+
console.log(num1);
1938+
console.log(num2);
1939+
```
1940+
1941+
- A: `10`, `10`
1942+
- B: `10`, `11`
1943+
- C: `11`, `11`
1944+
- D: `11`, `12`
1945+
1946+
<details><summary><b>Answer</b></summary>
1947+
<p>
1948+
1949+
#### Answer: A
1950+
1951+
The unary operator `++` _first returns_ the value of the operand, _then increments_ the value of the operand. The value of `num1` is `10`, since the `increaseNumber` function first returns the value of `num`, which is `10`, and only increments the value of `num` afterwards.
1952+
1953+
`num2` is `10`, since we passed `num1` to the `increasePassedNumber`. `number` is equal to `10`(the value of `num1`. Again, the unary operator `++` _first returns_ the value of the operand, _then increments_ the value of the operand. The value of `number` is `10`, so `num2` is equal to `10`.
1954+
1955+
</p>
1956+
</details>

0 commit comments

Comments
 (0)