Question 141 - 150
#141. What's the output?
const person = {
name: 'Lydia Hallie',
hobbies: ['coding'],
};
function addHobby(hobby, hobbies = person.hobbies) {
hobbies.push(hobby);
return hobbies;
}
addHobby('running', []);
addHobby('dancing');
addHobby('baking', person.hobbies);
console.log(person.hobbies);
javascript
- A:
["coding"]
- B:
["coding", "dancing"]
- C:
["coding", "dancing", "baking"]
- D:
["coding", "running", "dancing", "baking"]
Answer
Answer: C
The addHobby
function receives two arguments, hobby
and hobbies
with the default value of the hobbies
array on the person
object.
First, we invoke the addHobby
function, and pass "running"
as the value for hobby
and an empty array as the value for hobbies
. Since we pass an empty array as the value for hobbies
, "running"
gets added to this empty array.
Then, we invoke the addHobby
function, and pass "dancing"
as the value for hobby
. We didn't pass a value for hobbies
, so it gets the default value, the hobbies
property on the person
object. We push the hobby dancing
to the person.hobbies
array.
Last, we invoke the addHobby
function, and pass "baking"
as the value for hobby
, and the person.hobbies
array as the value for hobbies
. We push the hobby baking
to the person.hobbies
array.
After pushing dancing
and baking
, the value of person.hobbies
is ["coding", "dancing", "baking"]
#142. What's the output?
class Bird {
constructor() {
console.log("I'm a bird. π¦’");
}
}
class Flamingo extends Bird {
constructor() {
console.log("I'm pink. πΈ");
super();
}
}
const pet = new Flamingo();
javascript
- A:
I'm pink. πΈ
- B:
I'm pink. πΈ
I'm a bird. π¦’
- C:
I'm a bird. π¦’
I'm pink. πΈ
- D: Nothing, we didn't call any method
Answer
Answer: B
We create the variable pet
which is an instance of the Flamingo
class. When we instantiate this instance, the constructor
on Flamingo
gets called. First, "I'm pink. πΈ"
gets logged, after which we call super()
. super()
calls the constructor of the parent class, Bird
. The constructor in Bird
gets called, and logs "I'm a bird. π¦’"
.
#143. Which of the options result(s) in an error?
const emojis = ['π', 'π
πΌ', 'π', 'β'];
/* 1 */ emojis.push('π¦');
/* 2 */ emojis.splice(0, 2);
/* 3 */ emojis = [...emojis, 'π₯'];
/* 4 */ emojis.length = 0;
javascript
- A: 1
- B: 1 and 2
- C: 3 and 4
- D: 3
Answer
Answer: D
The const
keyword simply means we cannot redeclare the value of that variable, it's read-only. However, the value itself isn't immutable. The properties on the emojis
array can be modified, for example by pushing new values, splicing them, or setting the length of the array to 0.
#144. What do we need to add to the person
object to get ["Lydia Hallie", 21]
as the output of [...person]
?
const person = {
name: "Lydia Hallie",
age: 21
}
[...person] // ["Lydia Hallie", 21]
javascript
- A: Nothing, object are iterable by default
- B:
*[Symbol.iterator]() { for (let x in this) yield* this[x] }
- C:
*[Symbol.iterator]() { yield* Object.values(this) }
- D:
*[Symbol.iterator]() { for (let x in this) yield this }
Answer
Answer: C
Objects aren't iterable by default. An iterable is an iterable if the iterator protocol is present. We can add this manually by adding the iterator symbol [Symbol.iterator]
, which has to return a generator object, for example by making it a generator function *[Symbol.iterator]() {}
. This generator function has to yield the Object.values
of the person
object if we want it to return the array ["Lydia Hallie", 21]
: yield* Object.values(this)
.
#145. What's the output?
let count = 0;
const nums = [0, 1, 2, 3];
nums.forEach((num) => {
if (num) count += 1;
});
console.log(count);
javascript
- A: 1
- B: 2
- C: 3
- D: 4
Answer
Answer: C
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
.
#146. What's the output?
function getFruit(fruits) {
console.log(fruits?.[1]?.[1]);
}
getFruit([['π', 'π'], ['π']]);
getFruit();
getFruit([['π'], ['π', 'π']]);
javascript
- A:
null
,undefined
, π - B:
[]
,null
, π - C:
[]
,[]
, π - D:
undefined
,undefined
, π
Answer
Answer: D
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
.
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
.
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
offruits
, it returns undefined
since this item on index 1
does not exist.
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.
#147. What's the output?
class Calc {
constructor() {
this.count = 0;
}
increase() {
this.count++;
}
}
const calc = new Calc();
new Calc().increase();
console.log(calc.count);
javascript
- A:
0
- B:
1
- C:
undefined
- D:
ReferenceError
Answer
Answer: A
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
.
#148. What's the output?
const user = {
email: 'e@mail.com',
password: '12345',
};
const updateUser = ({ email, password }) => {
if (email) {
Object.assign(user, { email });
}
if (password) {
user.password = password;
}
return user;
};
const updatedUser = updateUser({ email: 'new@email.com' });
console.log(updatedUser === user);
javascript
- A:
false
- B:
true
- C:
TypeError
- D:
ReferenceError
Answer
Answer: B
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
.
#149. What's the output?
const fruit = ['π', 'π', 'π'];
fruit.slice(0, 1);
fruit.splice(0, 1);
fruit.unshift('π');
console.log(fruit);
javascript
- A:
['π', 'π', 'π']
- B:
['π', 'π']
- C:
['π', 'π', 'π']
- D:
['π', 'π', 'π', 'π']
Answer
Answer: C
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.
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 ['π', 'π']
.
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 ['π', 'π', 'π']
.
#150. What's the output?
const animals = {};
let dog = { emoji: 'πΆ' };
let cat = { emoji: 'π' };
animals[dog] = { ...dog, name: 'Mara' };
animals[cat] = { ...cat, name: 'Sara' };
console.log(animals[dog]);
javascript
- A:
{ emoji: "πΆ", name: "Mara" }
- B:
{ emoji: "π", name: "Sara" }
- C:
undefined
- D:
ReferenceError
Answer
Answer: B
Object keys are converted to strings.
Since the value of dog
is an object, animals[dog]
actually means that weβre creating a new property called "object Object"
equal to the new object. animals["object Object"]
is now equal to { emoji: "πΆ", name: "Mara"}
.
cat
is also an object, which means that animals[cat]
actually means that weβre overwriting the value of animals["object Object"]
with the new cat properties.
Logging animals[dog]
, or actually animals["object Object"]
since converting the dog
object to a string results "object Object"
, returns the { emoji: "π", name: "Sara" }
.