Skip to main content
Table of contents

Back to challenge 4

Solution

Challenge 4: Famous Writers

Long solution with a for loop

for (var person = 0; person < writers.length; person++) {
    var firstName = writers[person].firstName
    var lastName = writers[person].lastName
    var age = writers[person].age
    var occupation = writers[person].occupation

    console.log(`Hi, my name is ${firstName} ${lastName}. I am ${age} years old, and work as a ${occupation}.`);
}

Short solution with forEach

var whoAmI = writers.forEach(person =>
    console.log(`Hi, my name is ${person.firstName} ${person.lastName}. I am ${person.age} years old, and work as a ${person.occupation}.`))

Here some documentation about it.


BONUS solution

var aliveWriters = writers.forEach(person =>
    person.alive ? console.log(`I am ${person.firstName} and I am alive!`) : false)

Go to challenge 5

This page was last reviewed on 3 February 2022. It needs to be reviewed again on 3 February 2023 .
This page was set to be reviewed before 3 February 2023. This might mean the content is out of date.