-1

Depending on the input, the function should return an array of 'num' elements ranging from 'start' to 'end'. The 'type' argument determines whether they should be integers ('1') or floating point ('0'). First a new array of 'num' length is created and then there's a switch statement to determine if it should be integers or decimal numbers generated. I seem to be having problems with returning them. The functions generating random numbers I have taken from Mozilla's developer website. The whole think will be feed in React Native's action creator but I don't think that's relevant here.

export const genNum = (start, end, num, type) => {
  let numberArray = new Array(num);

  switch (type) {
    case 1:
      return numberArray.map(function(num) {
        let min = Math.ceil(start);
        let max = Math.floor(end);
        return Math.floor(Math.random() * (max - min)) + min;
      });
    default:
      return numberArray.map(function(num) {
        return Math.floor(Math.random() * (max - min)) + min;
    });
  }
};

EDIT: I don't think it's a duplicate as it tries to combine both cases. I'm still reading on reduce() to get rid of unnecessary switch statement. I've taken on board your corrections. At the moment it looks as follows (I still need to round the floating point numbers to 2dp).

export const genNum = (start, end, num, type) => {
  let numberArray = new Array(num).fill(-1);

  switch (type) {
    case 1:
      return numberArray.map(function(num) {
        let min = Math.ceil(start);
        let max = Math.floor(end);
        return Math.floor(Math.random() * (max - min)) + min;
      });
    default:
      return numberArray.map(function(num) {
        let min = start;
        let max = end;
        return (Math.random() * (max - min)) + min;
    });
  }
};
Wasteland
  • 4,889
  • 14
  • 45
  • 91
  • 2
    What do you mean by having problems returning them? When I read your code it seems like it returns the array just fine, but it is an array with no entries so `map` doesn't do anything on it (map runs the callback once for each entry, but when your array has no entries that means the callback never runs). The end result is returning an array with length equal to `num` and no entries. – Paul May 29 '17 at 22:11
  • This looks like a grossly over complicated `reduce()`. Why would you need a `switch` and duplication of `map()`? – charlietfl May 29 '17 at 22:12
  • Possible duplicate of [Generating random whole numbers in JavaScript in a specific range?](https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range) – num8er May 29 '17 at 22:17
  • The 'Possible duplicate' comment seems to be about generating a random number, not about returning an array. – tevemadar May 30 '17 at 07:04
  • @charlietfl I have been looking at reduce() but can't think of how I could apply it here to simplify the logic. Any hints? Thank you – Wasteland Jun 03 '17 at 11:29

1 Answers1

2

Some of your problems will be resolved if you properly initialize the array. You can't map over an array created as you are doing because it doesn't really contain any values. I suggest you change the line that defines your array to:

let numberArray = Array(num).fill(-1);

Then you can address some of the other problems in your code (like, min and max not being defined in the second case block, and the floating point part not working because you are rounding them to integers)

James
  • 20,957
  • 5
  • 26
  • 41