[Solved] Player (Simple Class)

  

5
Topic starter

Write a JavaScript class Player:

class Player {
// TODO: implement this class
}

Each player holds nickname and list of scores . Implement the following features:

  • Constructor(nickName) – creates a player with given nickname.
  • Method addScore(score) – adds a score (as number) to the scores list. If passed argument is not a valid number or a string representation of a number, do nothing (ignore it).
  • Accessor property scoreCount – returns the total amount of scores in the list.
  • Accessor property highestScore – returns the highest score (number) in the list.
  • Accessor property topFiveScore – gets the top 5 score (ordered descending), print all scores available if the score’s count is below or equal to 5.
  • Method toString() – returns the text representation of the player in the following format:

o  Player with only nickname:

{nickname}: []

Player with several scores (e.g. 450 and 200), ordered by descending:

{nickname}: [450,200]

Examples:
This is an example how the Player class is intended to be used:

Sample code usage:

let peter = new Player("Peter");
console.log('Highest score: ' + peter.highestScore);
console.log(`Top 5 score: [${peter.topFiveScore}]`);
console.log('' + peter);
console.log('Score count: ' + peter.scoreCount);
peter.addScore(450);
peter.addScore(200);
console.log('Highest score: ' + peter.highestScore);
console.log(`Top 5 score: [${peter.topFiveScore}]`);
console.log('' + peter);
peter.addScore(2000);
peter.addScore(300);
peter.addScore(50);
peter.addScore(700);
peter.addScore(700); 
console.log('Highest score: ' + peter.highestScore);
console.log(`Top 5 score: [${peter.topFiveScore}]`);
console.log('' + peter);
console.log('Score count: ' + peter.scoreCount);
console.log();
let maria = new Player("Maria")
.addScore(350)
.addScore(779)
.addScore(180);
console.log('Highest score: ' + maria.highestScore);
console.log(`Top 5 score: [${maria.topFiveScore}]`);
console.log('' + maria);

Corresponding output:

Highest score: undefined
Top 5 score: []
Peter: []
Score count: 0
Highest score: 450
Top 5 score: [450,200]
Peter: [450,200]
Highest score: 2000
Top 5 score: [2000,700,700,450,300]
Peter: [2000,700,700,450,300,200,50]
Score count: 7
Highest score: 779
Top 5 score: [779,350,180]
Maria: [779,350,180]
1 Answer
4

Here is the class Player and the solution to this task:

class Player {
    constructor(nickName) {
        this.nickName = nickName;
        this.scoreList = [];
    }
 
    addScore(score) {
        if(!isNaN(score) && score !== null){
            this.scoreList.push(+score);
            this.scoreList.sort((a,b) => b-a);
        }
 
        return this;
    }
 
    get scoreCount() {
        return this.scoreList.length;
    }
 
    get highestScore() {
        return this.scoreList[0];
    }
 
    get topFiveScore() {
        return this.scoreList.slice(0, 5);
    }
 
    toString() {
        return `${this.nickName}: [${this.scoreList}]`;
    }
}
Share: