Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active May 29, 2020 06:17
Show Gist options
  • Save sandrabosk/dd0d885265d10272936e935142fca28b to your computer and use it in GitHub Desktop.
Save sandrabosk/dd0d885265d10272936e935142fca28b to your computer and use it in GitHub Desktop.
// ***********************************************************************************************************************
// https://www.codewars.com/kata/coding-meetup-number-2-higher-order-functions-series-greet-developers
// You will be given an array of objects (associative arrays in PHP) representing data
// about developers who have signed up to attend the next coding meetup that you are
// organizing.
// Your task is to return an array where each object will have a new property
// 'greeting' with the following string value:
// Hi < firstName here >, what do you like the most about < language here >?
// For example, given the following input array:
// var list1 = [
// { firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 'Americas', age: 35, language: 'Java' },
// { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 35, language: 'Python' },
// { firstName: 'Madison', lastName: 'U.', country: 'United States', continent: 'Americas', age: 32, language: 'Ruby' }
// ];
// your function should return the following array:
// [
// { firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 'Americas', age: 35, language: 'Java',
// greeting: 'Hi Sofia, what do you like the most about Java?'
// },
// { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 35, language: 'Python',
// greeting: 'Hi Lukas, what do you like the most about Python?'
// },
// { firstName: 'Madison', lastName: 'U.', country: 'United States', continent: 'Americas', age: 32, language: 'Ruby',
// greeting: 'Hi Madison, what do you like the most about Ruby?'
// }
// ];
// ***********************************************************************************************************************
// solution 1:
function greetDevelopers(list) {
for (const elem in list) {
list[elem].greeting = `Hi ${list[elem].firstName}, what do you like the most about ${list[elem].language}?`;
}
return list;
}
// solution 2:
const greetDevelopers = list =>
list.map(elem => ((elem.greeting = `Hi ${elem.firstName}, what do you like the most about ${elem.language}?`), elem));
// solution 3:
const greetDevelopers = list =>
list.map(user => ({ ...user, greeting: `Hi ${user.firstName}, what do you like the most about ${user.language}?` }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment