1

The purpose of this codes is to create an array of one hundred block objects randomly selected but I'm not sure if this code will work

    var array_result = ();

    for( let i= 0; i<100; i++){
      var number=  parseInt (7* Math.Random());
      switch (number) {
        case 0:
          array_result.push (new Dirt ());
          break;
        case 1:
          array_result.push (new CobbleStone ());
          break;
        case 2:
          array_result.push (new sand ());
          break;
        case 3:
          array_result.push (new IronOre ());
          break;
        case 4:
          array_result.push (new Gravel ());
          break;
        case 5:
          array_result.push (new Coal ());
          break;
        case 6:
          array_result.push (new RedStone ());
          break;
        case 7:
          array_result.push (new CraftingTable ());
          break;
      }
    }

    console.log(array_result)
ide
  • 19,942
  • 5
  • 64
  • 106
  • 2
    JS is functional.. as much as it can be.. So you might insert your Constructor functions into an array and just pick according to a random index or shuffle the array and pick one by one, – Redu Mar 07 '17 at 23:21
  • Possible duplicate of [Generating random whole numbers in JavaScript in a specific range?](http://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range) –  Mar 09 '17 at 19:06

5 Answers5

1

First your interval will never reach 7, because random() gives result between <0,1). Moreover parseInt() parses a string and returns first number-integer, so in my opinion it would be better to use something different.

Try this:

//random number in closed interval (includes its endpoints)
function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

var number = getRndInteger(0, 7);
Zuza
  • 11
  • 1
0

In Javascript, use Math.random() instead of Math.Random().

Your parseInt call should include a radix of 10. You would not run into any unexpected results in this case since you have only 8 block objects, but it is best practice to specify it anyway (otherwise parseInt may try to parse the value as an octal instead of decimal value.)

The code you have will never produce case 7, since Math.random produces numbers in the range of 0 (inclusive) to 1 (exclusive).

To fix these issues, change:

var number = parseInt(7* Math.Random());

to

var number = parseInt(8 * Math.random(), 10);

Additionally:

To define an array, use square brackets instead of parentheses:

var array_result = [];
cherdt
  • 303
  • 4
  • 14
0

Here are the issues I see:

  1. var array_result = (); Change to

    var array_result = [];

  2. Math.random() returns an integer between 0 and 1. With the code you have, the ONLY way to get case 7 is if math.random returns 1. This is because parseInt(6.99999) would return 6. Change to

    var number= parseInt (8 * Math.Random());

  3. Use math.random() , no capital R

Asool
  • 13,031
  • 7
  • 35
  • 49
0

A more declarative approach to doing it:

const factories = [
    () => new Dirt(),
    () => new Cobblestone(),
    () => new Sand(),
    () => new IronOre(),
    () => new Gravel(),
    () => new Coal(),
    () => new RedStone(),
    () => new CraftingTable()
];

const getRandomNumber = ( maxNumber ) => parseInt(maxNumber * Math.random(), 10);

const getRandomFromArray = ( items ) => items[getRandomNumber(items.length)];

const getSomeItems = ( howMany, factories ) => new Array(howMany).map(() => getRandomFromArray(factories)());   

const array_result = getSomeItems(100, factories));
Alex Young
  • 4,009
  • 1
  • 16
  • 34
0

here's a neat compact way of switching on numbers without a switch, given 1 input and 1 output:

function createObject(typeId) {
    return new [ 
        Dirt, CobbleStone, Sand, IronOre, Gravel, Coal, RedStone, CraftingTable
    ][typeId] || Error;
}

you can even define/modify the array externally to edit the assignments in real time, something that's a monster to do with a switch. It's also faster than switch because it doesn't evaluate each condition until it finds a match: it jumps right to the correct slot and instantiates a new object of the type named by the constructor in that slot.

performance aside, i like the pattern because it's clean, readable, and easy to maintain.

dandavis
  • 16,370
  • 5
  • 40
  • 36