London-Class-7 - Omar Sharif - JavaScript - CORE -2 - WEEK - 1 - #47
London-Class-7 - Omar Sharif - JavaScript - CORE -2 - WEEK - 1#47omarsharif-gr wants to merge 1 commit into
Conversation
Completed Mandatory exercises
| ]; | ||
|
|
||
| function introductorySentence(writer){ | ||
| return `Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.` |
There was a problem hiding this comment.
Well done for using string interpolation correctly!
| const newArray = writers.map(introductorySentence); | ||
| console.log(newArray); |
There was a problem hiding this comment.
Here, a better method to use would be the forEach method. Similarly to map, it loops through the elements of the array it is acting on and executes the function passed to it as an argument. Differently to map, it doesn't store the return value of the function into an output array (in this case, newArray).
This means that you can use a function that doesn't return a value (also known as a void function in some programming languages) to pass to forEach. Try combining this with rewriting introductorySentence so that it doesn't return a string but rather prints it on the screen.
| for (let writersData of writers){ | ||
| if (writersData.age >= 40 && writersData.age <= 49 && writersData.alive === !true) { | ||
| console.log(`Writer ${writersData.firstName} ${writersData.lastName} died at ${writersData.age} years old.`); | ||
| } | ||
| } |
There was a problem hiding this comment.
Good use of the for ... of statement, and the conditional expression in the if statement.
As an alternative to this approach - do you see how you might be able to rewrite this piece of code using the forEach method?
|
|
||
| let thirtyFiveOrOlder = []; | ||
| let thirtyFiveOrOlder = [people.filter(value => value.age >= 35)]; | ||
|
|
There was a problem hiding this comment.
First of all good work Omar, however, i notice with your functions you are surrounding them with extra square brackets. This will return an array within an array, something like this --> [['x', 'y', 'z']], which is not what you're asked for. An additional point will be, although this looks cleaner, please consider readability too.
|
|
||
| let powerNetEmails = []; | ||
| let powerNetEmails = [people.filter(x.company === 'POWERNET').map(x=>x.email).reverse()]; | ||
|
|
There was a problem hiding this comment.
Over here, be careful since you have forgotten to set the x value as an argument in the filter function.
|
|
||
| let friendsWithStacie = []; | ||
| let friendsWithStacie = [people.filter(x => x.friends.find(x => x.name === 'Stacie Villarreal')).map(x=> `${x.name.first} ${x.name.last}`).reverse]; | ||
|
|
There was a problem hiding this comment.
Besides the extra square bracket, you are not calling the reverse method on here, which will cause an error.
| */ | ||
|
|
||
| let friendsWhoCanMultitask = []; | ||
| let friendsWhoCanMultitask = [people.filter(x => x.friends.filter(appropriateSkillSet.skills.includes('Multi-tasking'))).flat().map(x=> x.name)]; |
There was a problem hiding this comment.
Also, this function does not work, it returns the name of the friends in the list rather than filters the friends who can multitask
|
Your coursework submission has been closed because nobody has interacted with it in six weeks. You are welcome to re-open it to get more feedback. |
Completed Mandatory exercises
Your Details
Homework Details