1

I have a an array of objects and want to find the player with the highest score which also does not exceed a certain score (for example 20 points). How do I do so?

    let playerScores = [ 
  { player: 'Dealer',
    score: 19,
    cards: 'A of ♥, 5 of ♦' },
  { player: 'Player1',
    score: 18,
    cards: '10 of ♦, 8 of ♦' },
  { player: 'Player2',
    score: 23,
    cards: '6 of ♦, 6 of ♥, J of ♠' }
    ]

console.log(playerScores)
// iterates through playerScores
for (let i in playerScores) {
    if (playerScores[i].score > 20) {
        console.log(playerScores[i].player + ' BUSTED!')
        // removes the object with a score over 21 from the array
        playerScores.splice(i,1)
    }
    return playerScores
}
5
  • what does not work with the code? Commented Dec 30, 2017 at 21:09
  • I need to find the player with the highest score, which in this case would be Dealer with a score of 19. Commented Dec 30, 2017 at 21:11
  • yes, exactly! I want to find the player which has the highest score which is lower than 20. Commented Dec 30, 2017 at 21:23
  • what about ties? Commented Dec 30, 2017 at 21:33
  • then I would need to present all players who have the same score Commented Dec 30, 2017 at 21:39

5 Answers 5

3

You can filter the data first, sort it by score and then take the first one:

let filteredData = playerScores.filter(p=>p.score<=20).sort((a,b) => b.score-a.score);
let result = filteredData.length ? filteredData[0] : undefined;`
Sign up to request clarification or add additional context in comments.

Comments

1

A simple solution is to make a function that takes the limit score and iterate over the elements of palyerScores and find the match element look at the example below

let playerScores = [
    {
        player: 'Dealer',
        score: 19,
        cards: 'A of ♥, 5 of ♦',
    },
    {
        player: 'Player1',
        score: 18,
        cards: '10 of ♦, 8 of ♦',
    },
    {
        player: 'Player2',
        score: 23,
        cards: '6 of ♦, 6 of ♥, J of ♠',
    },
];
function getHighestScore(limitScore) {
    let highestScore = 0;
    let player = {};
    for (let i = 0; i < playerScores.length; i++) {
        if (
            playerScores[i].score > highestScore &&
            playerScores[i].score < limitScore
        ) {
            highestScore = playerScores[i].score;
            player = Object.assign({}, playerScores[i]);
        }
    }
    return player;
}
let highsetScorePlayerNotExced20 = getHighestScore(20);
console.log(highsetScorePlayerNotExced20);

Comments

1

You could use a chained check of conditions and add the object if necessary to the result set or replace the object with one with a higher score.

var playerScores = [{ player: 'Dealer', score: 19, cards: 'A of ♥, 5 of ♦' }, { player: 'Player1', score: 18, cards: '10 of ♦, 8 of ♦' }, { player: 'Player2', score: 23, cards: '6 of ♦, 6 of ♥, J of ♠' }],
    value = 20,
    result = playerScores.reduce(function (r, o) {
        if (o.score >= value) {
            return r;
        }
        if (!r.length || r[0].score < o.score) {
            return [o];
        }
        if (r[0].score === o.score) {
            r.push(o);
        }
        return r;
    }, []);

console.log(result);

Comments

0
let playerScores = [
    { player: 'Dealer',
        score: 19,
        cards: 'A of ♥, 5 of ♦' },
    { player: 'Player1',
        score: 18,
        cards: '10 of ♦, 8 of ♦' },
    { player: 'Player2',
        score: 23,
        cards: '6 of ♦, 6 of ♥, J of ♠' }
];

const maxLimit = 20;

const maxScorePlayer = playerScores.reduce((prev, current) => {
    if (current.score >= maxLimit) {
        return prev;
    }

    return (prev.score > current.score) ? prev : current
}, { });

You can check how reduce function works here

Comments

0

    let playerScores = [ 
      { player: 'Dealer',
        score: 19,
        cards: 'A of ♥, 5 of ♦' },
      { player: 'Player1',
        score: 18,
        cards: '10 of ♦, 8 of ♦' },
      { player: 'Player2',
        score: 20,
        cards: '6 of ♦, 6 of ♥, J of ♠' }
    ];

    var highestScore = Math.max.apply(Math,playerScores.map(function(o){
                                         score = 0;
                                         if(o.score != 20) {
                                             score = o.score;
                                         }
                                         return score;
                                     }));
                         
    console.log(highestScore);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.