Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
push an array item to an object. Javascript

This piece of code returns Error: player_array[step].domino_set is undefined. I can't figure out why. It seems like the variables are in a global scope so I don't see why it errors.

var player = {
  user: 'CPU',
  order: 0,
  board: 'string',
  domino_set: [],
  player_order: '0'
};

const domino_array = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];

var domino_arrayCopy = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];


var player_array = ['player0', 'player1', 'player2', 'player3'];

var player0 = Object.create(player);
var player1 = Object.create(player);
var player2 = Object.create(player);
var player3 = Object.create(player);

player0;
player0.user = "PC";
player1;
player2;
player3;

function diviArray() {

  function arrayAssign() {

    var random = Math.floor(Math.random() * domino_arrayCopy.length);
    var die = domino_arrayCopy[random];

    domino_arrayCopy.splice(die, 1);
    return this.die;
  }


  for (let step = 0; step < player_array.length; step++) {
    for (let innerStep = 0; innerStep < 7; innerStep++) {
      player_array[step].domino_set.push(arrayAssign());

    }
  }


}

diviArray()

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Object.create is behaving weirdly with that domino_set array in the object so, while I look into it further because I don't fully understand why, here's a solution that uses a class instead.

class Player {
  constructor() {
    this.user = 'CPU';
    this.order = 0;
    this.board = 'string';
    this.domino_set = [];
    this.player_order = '0';
  }
};

const dominoArray = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];

const player_array = [new Player(), new Player(), new Player(), new Player()];

function diviArray(dominoArray) {

  function arrayAssign() {
    var random = Math.floor(Math.random() * dominoArray.length);
    return dominoArray.splice(random, 1)[0];
  }

  for (let step = 0; step < player_array.length; step++) {
    for (let innerStep = 0; innerStep < 7; innerStep++) {
      player_array[step].domino_set.push(arrayAssign());
    }
  }
  console.log(player_array);

}

diviArray(dominoArray);

about 4 years ago · Juan Pablo Isaza Denunciar

0

The player_array should be an array of objects. Yours was an array of strings, that's why you got the error. Also, the domino_set push line wasn't behaving like expected, so I adjusted a bit the code.

const player = {
  user: 'CPU',
  order: 0,
  board: 'string',
  domino_set: [],
  player_order: '0'
};

const domino_array = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];

var domino_arrayCopy = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour', 'ZeroFive', 'ZeroSix', 'OneZero', 'OneOne', 'OneTwo', 'OneThree', 'OneFour', 'OneFive', 'OneSix', 'TwoTwo', 'TwoThree', 'TwoSix', 'ThreeThree', 'ThreeSix', 'FourTwo', 'FourThree', 'FourFour', 'FourSix', 'FiveTwo', 'FiveThree', 'FiveFour', 'FiveFive', 'FiveSix', 'SixSix'];


const player0 = Object.create(player);
const player1 = Object.create(player);
const player2 = Object.create(player);
const player3 = Object.create(player);

player0.user = "PC";
const player_array = [player0, player1, player2, player3];


function diviArray() {

  function arrayAssign() {

    const random = Math.floor(Math.random() * domino_arrayCopy.length);
    const die = domino_arrayCopy[random];

    domino_arrayCopy.splice(die, 1);
    return die;
  }


  for (let step = 0; step < player_array.length; step++) {
    let arr = []
    for (let innerStep = 0; innerStep < 7; innerStep++) {
      arr.push(arrayAssign());
    }
    player_array[step].domino_set = [...arr];
  }
  console.log(player_array)

}

diviArray()

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda