I have the following code and I do not really know how to do so that each player is saved in a list of players, that is, in array
For example, if I want to iterate over their own objects, I have to assign an ID to each player? It occurs to me that their id could be their index in array
, but if so, < strong> how do I assign ID, if I have not saved the player in an array yet?
function Jugador(name, edad, dinero) {
this.name = name;
this.edad = edad;
this.dinero = dinero;
}
function obtenerNombre() { // Obtengo el nombre del input
var obtenerN = document.getElementById("nombre").value;
return obtenerN;
}
function obtenerEdad() { // Obtengo la edad del input
var obtenerE = document.getElementById("edad").value;
obtenerE = parseInt(obtenerE);
return obtenerE;
}
function crearJugador() {
var jug = new Jugador(obtenerNombre(), obtenerEdad(), 0);
document.write(jug.name, jug.edad, jug.dinero);
}
<!DOCTYPE html>
<html>
<head>
<title>HTML5, CSS3 and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->
<style>
body {
background-color: #111111;
}
h1 {
color: white;
}
</style>
<form>
<input type="text" id="nombre" placeHolder="Nombre" />
<input type="text" id="edad" placeholder="edad" />
<input type="button" onClick="obtenerNombre(); obtenerEdad(); crearJugador();" value="Enviar mis datos!" />
</form>
<!-- End your code here -->
</body>
</html>