Brenden Baio
Brenden Baio

Reputation: 233

Trying to build an array that fills each position with a dice roll

Here is my current code:

const diceRoll = Math.trunc(Math.random() * 6) + 1;
const diceRollArr = Array.from ({length: 4}, (_, i) => diceRoll[i]);

console.log(diceRollArr); //Returns: (4)[undefined, undefined, undefined, undefined]

Can't wrap my head around what I'm missing. If I do:

const diceRoll = Math.trunc(Math.random() * 6) + 1;
    const diceRollArr = Array.from ({length: 4}, (_, i) => diceRoll);

It will generate the same dice roll for the given array length, so I need a way to do a dice roll for each index position. Any help would be greatly appreciated, thanks!

Upvotes: 0

Views: 238

Answers (4)

Greedo
Greedo

Reputation: 3549

You are accessing diceRoll as an array when it's a variable. Also you need diceRoll to be a function and call it at every iteration

const diceRoll = () => Math.trunc(Math.random() * 6) + 1;
const diceRollArr = Array.from ({length: 4}, diceRoll);

console.log(diceRollArr);

Upvotes: 2

Rickard Elimää
Rickard Elimää

Reputation: 7591

I recently made a die roll class. For some reason, I suspect that you're a tabletop roleplaying gamer, so this will be somewhat suited for you.

Just create a new DicePool class, and generate new rolls by calling the roll() method.

function randomize(max, min = 1) {
  return Math.floor(Math.random() * max) + min;
}

class DicePool {
  constructor(combinationStr) {
    let [amount, type, modification] = this.#breakdown(combinationStr);
    this.amount = amount;
    this.type = type;
    this.modification = modification;
  }

  roll() {
    let result = 0;
    for (var i = 0; i < this.amount; i++) {
      result += randomize(this.type);
    }

    result += this.modification;

    return result;
  }

  #breakdown(combinationStr) {
    let [diceStr, modification] = this.#stripModification(combinationStr);
    let [amount, type] = diceStr.split('d');

    return [amount, type, modification];
  }

  #stripModification(str) {
    if (str.indexOf('-') != -1) {
      let aritmic = 1;
      let breakArr = str.split('-');
      breakArr[aritmic] *= -1

      return breakArr;
    } else if (str.indexOf('+') != -1) {
      return str.split('+');
    }

    return [str, 0];
  }
}

const d6 = new DicePool('1d6');
const diceRollArr = Array.from ({length: 4}, () => d6.roll());

console.log(diceRollArr)

Upvotes: 0

KooiInc
KooiInc

Reputation: 122916

Try converting diceRoll to a function, and use it for mapping the elements of an Array:

const diceRoll = _ => Math.trunc(Math.random() * 6) + 1;
const diceRollArr = [...Array(4)].map(diceRoll);
console.log(`[${diceRollArr}]`);

Upvotes: 2

PEPEGA
PEPEGA

Reputation: 2283

You can use my favourite [...Array(x)].map(() => ...) to loop x times, then inside the loop generate a random number with Math.random

const numberOfRolls = 10
const rolls = [...Array(numberOfRolls)].map(() => (~~(Math.random() * 6) + 1))
console.log(rolls)

Upvotes: 0

Related Questions