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;
});
}
};