Created
February 19, 2016 13:01
-
-
Save svschannak/0b1e67560a035d836f85 to your computer and use it in GitHub Desktop.
Codefights Maximizepoints Challenge
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
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