5 kyu
Fame Of All
Description:
Bad news ! Top places are ALL occupied by the same player ! ! ! ( som'thin' like myjinKing, myKingxin, or whatosoever ).
Things should change now !
Task
Create a class HallOfFame(size, players)
with list
property and add
method.
list
give list ofsize
players
in the form "Name: score", sorted by score in descending order and name in ascending order in case of same score.add( player )
lets. . . add a player to the list.- a
player
is a 2-items array :[name, score]
- if
score
is lesser than last player of the list then player is not added. - if
name
is yet in list,score
of this player is updated if new score is better than previous and other players may stay in list.
- if
- if a new player is added or a score is changed the list should be re-sorted
- at last,
add
method return the object itself (to permit chaining methods).
- a
HallOfFame
constructor may have 2 arguments :size
andplayers
.size
(default 5) defines the maximum size of thelist
(list
can't take more thansize
players)players
if provided, should be added to the list.- If no players (or not enough players) are provided "empty slots" should returns empty strings.
Examples
var top3 = new HallOfFame(3, [["Ada",99], ["Bob","42], ["Clo", 101], ["Dan", 3]])
// ... create a Hall of size 3 and (try) to add 4 players
top3.list -> ["Clo: 101", "Ada: 99", "Bob: 42"]
// ... only 3 players kept 'cause size of the Hall is 3
top3.add(["Dan",54])
top3.list -> ["Clo: 101", "Ada: 99", "Dan: 54"]
// ... Dan entered the list 'cause is score is better than Bob's
top3.add(["Eva",75]).add(["Fox",120]).list --> ["Fox: 120","Clo: 101","Ada: 99"]
// ... 2 new players added using chaining
var top5 = new HallOfFame();
// ... create an empty Hall of size 5 (by default)
top5.add(["A",4]).add(["E",3]).add(["I",1])
// ... add 3 players
top5.list --> ["A: 4","E: 3","I: 1","",""]
// ... 2 "empty players" at the end of list
top5.add(["S",5]).add(["T",7])
top5.list --> ["T: 7","S: 5","A: 4","E: 3","I: 1"]
// ... 2 more players, no more "empty slot"
top5.add(["A", 25]).list --> ["A: 25","T: 7","S: 5","E: 3","I: 1"]
// ... add "A" with a new (better) score then it moves to 1st place !
top5.add(["T", 6]).list --> ["A: 25","T: 7","S: 5","E: 3","I: 1"]
// ... try to add "S" with a lesser score then no change !
Note: The ECMAScript 2015 Class model given in "Initial Soluion" is not mandatory, feel free to choose the way to implement HallOfFame
.
Will you enter it ?
Arrays
Fundamentals
Similar Kata:
Stats:
Created | Aug 8, 2017 |
Published | Aug 8, 2017 |
Warriors Trained | 260 |
Total Skips | 0 |
Total Code Submissions | 1707 |
Total Times Completed | 96 |
JavaScript Completions | 96 |
Total Stars | 17 |
% of votes with a positive feedback rating | 89% of 32 |
Total "Very Satisfied" Votes | 27 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 6 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |