The Player Factory
JavaScript
10 pts
The Player Factory
Write a constructor function Player(name, number) that sets this.name, this.number and this.goals = 0.
Create p1 for Abel (number 7) and p2 for Senait (number 10) with new. Senait scores twice: add 2 to p2.goals. Print both objects. Expected output:
{ name: "Abel", number: 7, goals: 0 }
{ name: "Senait", number: 10, goals: 2 }
Checklist
- Write the constructor
Player(name, number). - Create
p1andp2withnew. - Give Senait 2 goals (without writing objects by hand).
- Print both players.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
