JavaScript Object help

var playerList = new Array();

function addPlayer() {
var newPlayer = playerList.length;
// ADD: check to see if the player’s first AND last name has been entered,
// if not, print an alert message.
if ((document.forms[0].elements[12].value == “”) && (document.forms[0].elements[13].value == “”)){
window.alert(“You must enter the contact’s first and last names.”);
}else if((!document.forms[0].elements[12].value == “”) && (!document.forms[0].elements[13].value == “”)){
}
// Here is where I get lost. ADD: Create a new Object and assign it to playerList[newPlayer]
// playerList[newPlayer].lastName = document.forms[0].elements[12].value;
// Assign each attribute of the Player (see commented line above for an example)

	if (document.forms[0].elements[20].checked == true)
		playerList[newPlayer].permission = true;
	else
		playerList[newPlayer].permission = false;
	var createPlayer = new Option();
	createPlayer.value = playerList[newPlayer].lastName + ", " + playerList[newPlayer].firstName;
	createPlayer.text = playerList[newPlayer].lastName + ", " + playerList[newPlayer].firstName;
	document.forms[0].players.options[newPlayer] = createPlayer;
}

That’s not the best format option to ask for help, it’s not very clear man :confused:

// Here is where I get lost. ADD: Create a new Object and assign it to playerList[newPlayer]

 playerList[newPlayer].lastName = document.forms[0].elements[12].value;

Anyway, is the above line the problem? At the beginning of the code you have:
var playerList = new Array();
and
var newPlayer = playerList.length;

It means that you are adding an element at the end of the array ( which length is should be the number of the actual players), adding a property to that element ( it must exist) and assigning it the value of the input box ( I guess ) corresponding to the 12th(13th) element of the array.
There are a lot of weird stuff in there btw, you should correct it step by step ( new player = length -1, if statements, the elements of the array…)

Layer, I am sorry, you’re right it is unclear. This is only a section of the code. I have declared it. I was just wasn’t understanding what the create new object meant. I tried several different ways and the debugger didn’t like it. Thank you for your guidance.

1 Like

I tried several different ways and the debugger didn’t like it.

I tried to refactor your code a bit, trying to respect the ‘layout’ of your code.

// global ( relative to this context ) array of player
const playerList = [];

// function declaration: you can pass in three parameters that you will 
// use throughout the function. 
function addPlayer(firstname, lastname, auth) {
// Declare a new player as an empty object: it will be added at the list
// at the end
const newPlayer = {};

// If either the firstname OR the lastname are empty string it pops the alert 
// and end the function
if (firstname === "" || lastname === "") {
window.alert("You must enter the contact’s first and last names.");
} else {
  // define a new property named 'firstName' for the empty object declared at the beginning 
  // of the function and it assign the value 'firstname' passed to the function
  newPlayer.firstName = firstname;

  // Same as above
  newPlayer.lastName = lastname;

  // Same as above
  newPlayer.permission = auth;

  // Log the result in browser console ( usually ctrl + shift + I )
  console.log(newPlayer.firstName);
  console.log(newPlayer.lastName);
  console.log(newPlayer.permission);

  // It push the newPlayer just created into the array
  playerList.push(newPlayer);
  }
};

// added few test players. To respect your code you can call it as
// addPlayer(document.forms[0].elements[12].value, document.forms[0].elements[13].value, document.forms[0].elements[20].checked)
// Assign a variable to document.forms[0].elements will save you a lot of typing:
// const userData = document.forms[0].elements;
// addPlayer(userData[12].value, userData[13].value, userData[20].checked)
// You could extract the whole form data using formData but i'm unsure what you're trying to achieve

addPlayer('testFirst', 'testLast', 'true');
addPlayer('secondFirst', 'secondLast', 'false');
addPlayer();

Happy coding! :slight_smile: