Skip to content

Instantly share code, notes, and snippets.

@svschannak
Created February 19, 2016 13:01
Show Gist options
  • Save svschannak/0b1e67560a035d836f85 to your computer and use it in GitHub Desktop.
Save svschannak/0b1e67560a035d836f85 to your computer and use it in GitHub Desktop.
Codefights Maximizepoints Challenge
function maximizePoints(team1, team2) {
var team2_copy = team2;
var len_team2 = team2.length;
best_points = 0;
for (i = 0; i < len_team2; i++){
highest_number = Math.max(...team2_copy);
index_of_highest_number = team2_copy.indexOf(Math.max.apply(Math, team2_copy));
higher_array = [];
team1.forEach(function(element, index, array){
if(element >= highest_number){
higher_array.push(element);
}
});
result = 0;
if(higher_array.length > 0){
best_result_number = Math.max(...higher_array);
index_of_best_result_highest_number = team1.indexOf(Math.max.apply(Math, higher_array));
team1.splice(index_of_best_result_highest_number, 1);
if(best_result_number > highest_number){
result = 1
}
}
best_points += result;
team2_copy.splice(index_of_highest_number, 1);
}
return best_points;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment