-
-
Save sethschori/7993782d2ee334ad878288edb03bfeda to your computer and use it in GitHub Desktop.
https://repl.it/CU8a/203 created by sethopia
This file contains hidden or 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
/* | |
EAT IT! | |
Create a function that will 'judge' the results of a competitive eating competition | |
There are 3 foods: | |
pizza: 2 points | |
hotdog : 3 points | |
cheeseburger : 5 points | |
Your function will take an array of objects. Each object contains the competitor's name, and the number of each food item they've eaten. | |
You should return an array of objects, with each object containing the player's name and their total score | |
eg: | |
Input: [ | |
{name: "Habanero Harry", pizza: 5 , hamburgers: 17, hotdogs: 11}, | |
{name: "Big Bob" , pizza: 20, hamburgers: 4, hotdogs: 11} | |
] | |
Output: [ | |
{score: 134, name: "Big Bob"}, | |
{score: 98, name: "Habanero Harry"} | |
] | |
*/ | |
var competitors = [ | |
{name: "Big Bernie" , pizza: 20, cheeseburger: 4, hotdog: 11}, | |
{name: "Habanero Hillary" , pizza: 2, cheeseburger: 14, hotdog: 3}, | |
{name: "Doughboy Donald" , pizza: 18, cheeseburger: 2, hotdog: 12}, | |
{name: "Creepy Cruz" , pizza: 1, cheeseburger: 1, hotdog: 0} | |
] | |
function judgeCompetitors(arr) { | |
var outputArr = []; | |
for (var i = 0; i < arr.length; i++) { | |
var thisCompetitor = arr[i]; | |
var thisScore = thisCompetitor.pizza * 2 + thisCompetitor.cheeseburger * 5 + thisCompetitor.hotdog * 3; | |
var outputObj = { | |
"score": thisScore, | |
"name": thisCompetitor.name | |
}; | |
outputArr.push(outputObj); | |
} | |
return outputArr; | |
} | |
console.log(judgeCompetitors(competitors)); |
This file contains hidden or 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
Native Browser JavaScript | |
>>> [ { score: 93, name: 'Big Bernie' }, | |
{ score: 83, name: 'Habanero Hillary' }, | |
{ score: 82, name: 'Doughboy Donald' }, | |
{ score: 7, name: 'Creepy Cruz' } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment