• Theory
  • Theory

Tenth program: “Golden ball”

Muffin respects football, but sometimes he is too lazy busy to watch games. But he doesn’t worry, because it’s enough for a cat to know the names of the best scorers and their effectiveness in order to bet on a player to keep the conversation going at the right time.

While you were writing the game, a letter from Muffin arrived. It was marked “I needed it done yesterday”.

Meow! I need a program that will calculate the usefulness and effectiveness of players based on their statistics. Design the code as a getStatistics function with one parameter: an array of players.

Each football player in this array is described by an object with three fields: name (property name), goals scored (property goals) and goal passes (property passes).

The function must return the same array, in which each player has two more fields added: effectiveness coefficient as per Muffin® (property coefficient) and performance (property percent).

  • The effectiveness coefficient is calculated like this: multiply the player’s goals by 2 (because I think that goals are the most important thing) and add all passes by the football player that resulted in a goal to this value.
  • The effectiveness (percentage of goals scored by a football player against the result of the whole team) is as follows: we determine the sum of the goals of all the players and find out how many percent of this number each football player scored. Round the value using Math.round().

Let’s calculate what percentage the number is:

// Total value, that is 100%
1200
// How many percent is 225 from 1200
(225 * 100) / 1200 = 18.75
// After rounding with Math.round()
19

When you are done, press Hey boss, here’s your program! in the console, and Muffin will call function getStatistics and test the results of your work. Good luck!

  • index.html
HTML
HTML

You’ve gone to a different page

Click inside the mini-browser to shift the focus onto this window.

100%
Granny Muffin

Easy there, pal!

To access the Objects challenges in the JavaScript programming, you need to sign up and subscribe first.