-1

I have multiple problems but my first is that my code returns an error:

"Uncaught TypeError: p1 is undefined".  

var 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 realDomino = [];
// function double(a, b){ if(a === b) { return true;}  else if (a !== b) { return false;} }
var Isdouble = function(p1, p2) {
  return p1.localeCompare(p2);
}

for (let x = 0; x < domino_array.length; x++) {
  var domino_die = {
    die: this.domino_array[x],
    TopDie: this.domino_array[x].split(/(?=[A-Z])/)[0],
    BottomDie: this.domino_array[x].split(/(?=[A-Z])/)[1],
    SpriteName: this.domino_array[x],
    Double: Isdouble(this.TopDie, this.BottomDie)
  };

  realDomino[x] = this.Object.create(domino_die);
}

The JavaScript examples I find seem to be too advanced for me.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
BlueConch
  • 1
  • 2
  • Duplicate of [Self-references in object literals / initializers](https://stackoverflow.com/q/4616202/4642212). If these are the examples that you say are too advanced for you, then see [ask]: _“Search, and research and keep track of what you find. Even if you don't find a useful answer elsewhere on the site, including links to related questions that haven't helped can help others in understanding how your question is different from the rest.”_ Be specific as to what you don’t understand. – Sebastian Simon Mar 26 '21 at 22:46
  • Thank you. This works, and solves the rest of my problems so far. I took a Javascript course but it's the first time I've seen get. Info for GET: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get – BlueConch Mar 26 '21 at 23:04

1 Answers1

0

You have to take care about references.

const dominoArray = ['ZeroZero', 'ZeroTwo', 'ZeroThree', 'ZeroFour'];
const realDomino = dominoArray.map(domino => {
   const item = {
    die: domino,
    TopDie: domino.split(/(?=[A-Z])/)[0],
    BottomDie: domino.split(/(?=[A-Z])/)[1],
    SpriteName: domino
   };
   item.Double = (item.TopDie && item.BottomDie) ? item.TopDie.localeCompare(item.BottomDie) : 0;
   return item;
});
console.log(realDomino);

Output will be:

0: {die: "ZeroZero", TopDie: "Zero", BottomDie: "Zero", SpriteName: "ZeroZero", Double: 0}
1: {die: "ZeroTwo", TopDie: "Zero", BottomDie: "Two", SpriteName: "ZeroTwo", Double: 1}
2: {die: "ZeroThree", TopDie: "Zero", BottomDie: "Three", SpriteName: "ZeroThree", Double: 1}
3: {die: "ZeroFour", TopDie: "Zero", BottomDie: "Four", SpriteName: "ZeroFour", Double: 1}
Akif Hadziabdic
  • 2,692
  • 1
  • 14
  • 25