Question
JavaScript Assignment Week 5 How do I add an array to this code? class Player { //player class to describe name and postion constructor(name, position)
JavaScript Assignment Week 5
How do I add an array to this code?
class Player { //player class to describe name and postion
constructor(name, position) { //parameters to hold name and position
this.name = name;
this.position = position;
}
describe() { //describes the player
return `${this.name} plays ${this.position}.`; //template literal for our return
}
}
class Team {
constructor(name) {
this.name = name;
this.players = []; //blank array to hold all players
}
addPlayer(player) {
if (player instanceof Player) { //instanceof - to hold array of players, not just a string or number, but a player
this.players.push(player); //push a player to it
} else { //if not player, throw error
throw new Error(`You can only add an instance of Player. Argument is not a player: ${player}`);
}
}
describe() { //describes the team
return `${this.name} has ${this.players.length} players.`; //template literal for name and the length (how many) of players
}
}
class Menu {
constructor() { //not take any argument unlike Team and Player
this.teams = []; //an array of teams
this.selectedTeam = null; //create a variable for team we have selected and manage one team at a time; null - no team is selected
}
start() { //entry point to our application
let selection = this.showMainMenuOptions();
while (selection != 0) {
switch (selection) {
case '1': //if selects 1, then create team
this.createTeam();
break;
case '2': //if selects 2, view a specific team
this.viewTeam();
break;
case '3': //if 3, then delete team
this.deleteTeam();
break;
case '4': //if 4, then display all teams
this.displayTeams();
break;
default: //if anything else, set selection to zero
selection = 0;
}
selection = this.showMainMenuOptions(); //get selection outside our switch, but inside our loop
}
alert('Goodbye!'); //if selects zero
}
showMainMenuOptions() {
return prompt(`
0) exit
1) create new team
2) view team
3) delete team
4) display all teams
`);
}
showTeamMenuOptions(teamInfo) {
return prompt(`
0) back
1) create player
2) delete player
--------------------
${teamInfo}
`);
}
displayTeams() {
let teamString = ''; //use a blank string, so we can build a string
for (let i = 0; i < this.teams.length; i++) { //i less than the length of teams, iterate through our teams
teamString += i + ') ' + this.teams[i].name + ' '; //concatenate variable, create team and new line for each one
}
alert(teamString); //outside our for loop
}
createTeam() {
let name = prompt('Enter name for new team:'); //prompt name
this.teams.push(new Team(name)); //push to our teams array
}
viewTeam() { //view team method
let index = prompt('Enter the index of the team you wish to view:'); //what team they wish to view
if (index > -1 && index < this.teams.length) {
this.selectedTeam = this.teams[index]; //validated teams
let description = 'Team Name: ' + this.selectedTeam.name + ' '; //description = concatenate
for (let i = 0; i < this.selectedTeam.players.length; i++) { //length of players from selectedTeam
description += i + ') ' + this.selectedTeam.players[i].name
+ ' - ' + this.selectedTeam.players[i].position + ' ';
}
let selection = this.showTeamMenuOptions(description);
switch (selection) { //selection is different, submenu
case '1': //create a new player
this.createPlayer();
break;
case '2': //delete a player
this.deletePlayer();
}
}
}
deleteTeam() {
let index = prompt('Enter the index of the team you wish to delete:');
if (index > -1 && index < this.team.length) {
this.teams.splice(index, 1);
}
}
createPlayer() {
let name = prompt('Enter name for new player:');
let position = prompt('Enter position for new player:');
this.selectedTeam.players.push(new Player(name, position));
}
deletePlayer() {
let index = prompt('Enter the index of the player you wish to delete:');
if (index > -1 && index < this.selectedTeam.players.length) { //validate it
this.selectedTeam.players.splice(index, 1); //move at 1
}
}
}
let menu = new Menu();
menu.start();
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started