Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create rankingService.js #33

Merged
merged 2 commits into from
Jan 14, 2021

Conversation

memeraki
Copy link
Collaborator

No description provided.

Comment on lines 84 to 85
currentMode.sort( (a,b) => b.timeInSeconds - a.timeInSeconds );
currentMode.sort( (a,b) => b.score - a.score );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two separate sorts are bad - you cannot be sure that sorting is stable.
More on sort stability and what it even is: https://medium.com/@fsufitch/is-javascript-array-sort-stable-46b90822543f

It is better to make one sorting function, in example:

(a,b) => {
 const scoreCompare = b.score - a.score;
 if(scoreCompare != 0) { // if scores are different, then it is all we want.
   return scoreCompare;
 } else { // if score is the same, then we need to compare only time of those elements.
   return b.timeInSeconds - a.timeInSeconds;
 }
}

currentMode = PokemonApiRanking.mode3.scores;
break;
default:
trow `Wrong mode`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw*, typo

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the best is to use throw new Error('message')

// check localStorage for current Top scores
// if empty create object with objects mode1, mode2, and mode3
// PokemonApiRanking - data from/to localStorage
const PokemonApiRanking = JSON.parse(localStorage.getItem('PokemonApiRanking')) || {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should get it every time you call rankingService function, not only at the start.

If you once add score to localStorage then this PokemonApiRanking is updated, okay... but the second score that would be added is based not on the content of the local storage (that could be somehow changed between first and second game), but is based on the local copy you have in this variable. (Not sure if this is clear enough - if not - contact me)

Comment on lines 40 to 45
currentMode.push(user);
currentMode.sort( (a,b) => b.timeInSeconds - a.timeInSeconds );
currentMode.sort( (a,b) => b.score - a.score );
if(currentMode.length > 3) {
currentMode.length = 3;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this approach looks better for me

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but setting array length is not good 🤮

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best thing would be to:

  1. add new record to old array
  2. sort it
  3. create new array
  4. add only 3 first elements of old array to new array
  5. set "new array" as right property of whole PokemonApiRanking

Comment on lines 56 to 64
export const printTopUsers = (users = [], htmlList) => {
htmlList.innerHTML = users.map((user, i) => {
return `
<li>
<label for="item${i}"><strong>${user.name}</strong>: ${user.score}points in ${user.timeInSeconds}seconds</label>
</li>
`;
}).join('');
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

displaying records and whole rankingService functionality should be in separate files (separation of responsibilities)

Copy link
Owner

@lukaszdutka lukaszdutka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed 😊 please make changes

some small changes and:
- removed print to html function
- created update from localStorage function
Copy link
Owner

@lukaszdutka lukaszdutka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Good job! :D

@lukaszdutka lukaszdutka merged commit f9621a6 into dev Jan 14, 2021
@lukaszdutka lukaszdutka deleted the MNG-19-Create-src/service/rankingService.js branch January 14, 2021 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants