Code Lab

Worker class and average salary

HardClasses20 min

Write a class Worker with the fields name (string), age (number) and salary (number).

  • Constructor: new Worker(name, age, salary).
  • A getter and a setter for every field: worker.salary returns the salary, worker.salary = 2000 changes it. Use get and set, not plain properties.
  • toString() returns a string exactly like Worker{name='Ann', age=30, salary=1500}. Numbers are printed the default JavaScript way: 1500.5, not 1500.50.
  • The function averageSalary(workers) takes an array of Worker objects and returns the average salary, not rounded. For an empty array it returns 0.
  • Create an array workers with at least three Worker objects.

Examples

new Worker('Ann', 30, 1500).toString()

Returns "Worker{name='Ann', age=30, salary=1500}"

averageSalary([new Worker('Ann', 30, 1000), new Worker('Bob', 41, 2000)])

Returns 1500

averageSalary([])

Returns 0

Asked at EPAM

Loading the editor...
  1. The constructor stores the fields, getters return them
  2. Setters change the values
  3. name, age and salary are get/set accessors
  4. toString format
  5. Average salary of two workers
  6. Empty list: 0
  7. The workers array holds Worker objects

Hidden tests run on Submit.