Skip to content

Instantly share code, notes, and snippets.

@jrobber
Created October 21, 2015 21:55
Show Gist options
  • Save jrobber/d5437cb4e8b04809c81c to your computer and use it in GitHub Desktop.
Save jrobber/d5437cb4e8b04809c81c to your computer and use it in GitHub Desktop.
Counting Cover Bands
var coverBands = [
{
contact: "[email protected]",
name: "Queen"
},
{
contact: "[email protected]",
name: "Journey"
},
{
contact: "[email protected]",
name: "Elvis"
},
{
contact: "[email protected]",
name: "Queen"
},
{
contact: "[email protected]",
name: "Elvis"
},
{
contact: "[email protected]",
name: "Elvis"
},
{
contact: "[email protected]",
name: "Britney Spears"
},
{
contact: "[email protected]",
name: "Journey"
}
]
// for var i in bands
//Store : Band name
// Count of occurances
function countBands(bands){
var bandsCount = {
// Queen: 2,
// Journey: 2,
// Elvis: 3,
};
for(var i = 0; i < bands.length; i++){
var band = bands[i];
if(bandsCount.hasOwnProperty(band.name)){
bandsCount[band.name]++;
} else {
bandsCount[band.name] = 1;
}
}
}
function countBands2(bands){
var bandsCount = [
// {
// name: "Queen",
// count: 2
// }
]
for(var i = 0; i < bands.length; i++){
var band = bands[i];
var found = false;
for(var k = 0; k < bandsCount.length; k++){
var countedBand = bandsCount[k];
if(countedBand.name === band.name){
found = true;
countedBand.count++;
}
}
if(!found){
bandsCount.push({
name: band.name,
count: 1
});
}
}
return bandsCount;
}
var finalCount = countBands2(coverBands);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment