This repository was archived by the owner on Jan 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy path1-writers.js
More file actions
67 lines (56 loc) · 2.14 KB
/
1-writers.js
File metadata and controls
67 lines (56 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Challenge 1: Famous Writers
Did you know you can also have an array of objects? We've created one for you here. Loop through the array,
and for each object, `console.log()` out the sentence:
"Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}."
Here is the array:
*/
let writers = [
{
firstName: "Virginia",
lastName: "Woolf",
occupation: "writer",
age: 59,
alive: false
},
{
firstName: "Zadie",
lastName: "Smith",
occupation: "writer",
age: 41,
alive: true
},
{
firstName: "Jane",
lastName: "Austen",
occupation: "writer",
age: 41,
alive: false
},
{
firstName: "bell",
lastName: "hooks",
occupation: "writer",
age: 64,
alive: true
}
];
// https://reactgo.com/javascript-loop-through-array-of-objects/#:~:text=In%20es6%20we%20have%20a,over%20the%20array%20of%20objects.&text=forEach%20methods%20takes%20the%20callback,object%20present%20in%20the%20array.
// writers.forEach((writer) => console.log(writer.firstName, writer.lastName)); --> to test if this forEach method works
// METHOD1: forEach methods takes the callback function as an argument and runs on each object present in the array.
writers.forEach((writer) => console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`));
// METHOD2: in this for of loop, on each iteration different object is assigned to the writer variable.
for(let writer of writers) {
console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`);
}
/*
If you want an extra challenge, only `console.log()` the writers that are alive.
*/
// https://alligator.io/js/filter-array-method/
function aliveWriter(writer) {
if (writer.alive === true) {
return writer;
}
}
let aliveWriters = writers.filter(aliveWriter);
// console.log(aliveWriters); ---> to check if the output is correct.
aliveWriters.forEach((writer) => console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`));