• 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 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 players’ effectiveness coefficients and goal percentages based on their statistics. Write 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 assists (passes that led to goals) (property passes).

The function must return the same array, in which each player has two more fields added: the Muffin® effectiveness coefficient (property coefficient) and percentage of the team’s goals (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 the number of the player’s passes 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 what percentage of this total each football player scored. Round the value using Math.round().

Assigning a value to a property that does not exist yet adds it to the object:

var cat = {name: 'Muffin'};
cat.age = 5;

Let’s express one number as a percentage of the total:

// Total value, that is 100%
1200
// 225 as a percentage of 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 first.