Created
February 28, 2019 12:20
-
-
Save AndriiBozh/4e0e65de833eccf0276123751073757f to your computer and use it in GitHub Desktop.
CodeWars: find the 1st Python dev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASSIGNMENT | |
_______________________ | |
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 organising. | |
The list is ordered according to who signed up first. | |
Your task is to return one of the following strings: | |
< firstName here >, < country here > of the first Python developer who has signed up; or | |
There will be no Python developers if no Python developer has signed up. | |
Notes: | |
The input array will always be valid and formatted as in the example above. | |
_______________________ | |
SOLUTION | |
_______________________ | |
function getFirstPython(list) { | |
for (var i = 0; i < list.length; i++) { | |
if (list[i].language === 'Python') { | |
return `${list[i].firstName}, ${list[i].country}`; | |
} | |
} | |
return 'There will be no Python developers'; | |
}; | |
getFirstPython([ | |
{ firstName: 'Mark', lastName: 'G.', country: 'Scotland', continent: 'Europe', age: 22, language: 'JavaScript' }, | |
{ firstName: 'Victoria', lastName: 'T.', country: 'Puerto Rico', continent: 'Americas', age: 30, language: 'Python' }, | |
{ firstName: 'Emma', lastName: 'B.', country: 'Norway', continent: 'Europe', age: 19, language: 'Clojure' } | |
]) | |
________________________ | |
Other solution: | |
function getFirstPython(list) { | |
let match = list.find((obj) => obj.language === 'Python'); | |
return match ? `${match.firstName}, ${match.country}` : 'There will be no Python developers'; | |
}; | |
getFirstPython([ | |
{ firstName: 'Mark', lastName: 'G.', country: 'Scotland', continent: 'Europe', age: 22, language: 'JavaScript' }, | |
{ firstName: 'Victoria', lastName: 'T.', country: 'Puerto Rico', continent: 'Americas', age: 30, language: 'Python' }, | |
{ firstName: 'Emma', lastName: 'B.', country: 'Norway', continent: 'Europe', age: 19, language: 'Clojure' } | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment