Inline Arrays vs. IF statements

IF statements

Pros:

  • ✅ Explicit control over value ranges and logic.
  • ✅ Easier to read when conditions aren't linear or rely on non-uniform logic.
  • ✅ Good for one-off or simple situations where a full table is overkill.
  • Cons:

  • ❌ Can get long and repetitive.
  • ❌ Harder to refactor or scale for different mappings.












  • Inline arrays

    Pros:

    Cons:

    Javascript

    // V.1 Generates an attribute score based on a weighted mapping of "IF" statements

    function generateAttribute() {

    const roll = getRandomInt(0, 19);

    if (roll <= 1) return 30;

    if (roll <= 3) return 35;

    if (roll <= 6) return 40;

    if (roll <= 10) return 45;

    if (roll <= 13) return 50;

    if (roll <= 15) return 55;

    if (roll <= 17) return 60;

    if (roll === 18) return 65;

    return 70; // roll === 19

    }


    // V.2 Alternate version: using a mapping array to preserve probabilities and elegance

    function generateAttribute() {

    const table = [

    30, 30, // 0, 1

    35, 35, // 2, 3

    40, 40, 40, // 4, 5, 6

    45, 45, 45, 45, // 7-10

    50, 50, 50, // 11-13

    55, 55, // 14-15

    60, 60, // 16-17

    65, // 18

    70 // 19

    ];

    const roll = getRandomInt(0, 19);

    return table[roll];

    }