Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!