sampleArr

Generates an array of random numbers.

1.
/**
2.
* Generates an array of random numbers.
3.
**/
4.
const sampleArr = (size: number) => {
5.
if (size < 0) throw new Error('Size must be a positive number');
6.
return Array.from({ length: size }, (_, i) => Math.floor(Math.random() * i));
7.
};
8.
9.
export default sampleArr;

1. Installtion

npx @jrtilak/lazykit add sampleArr

2. Parameters

  • size (number)
    The size of the array to be generated. It must be a positive number.

3. Returns

  • number[]
    Returns an array of random numbers, where the length of the array is determined by the size parameter.

4. Errors

  • Throws an error if size is less than zero, indicating that a positive number is required.

5. Usage

1. Positive Index

1.
import sampleArr from "@/helpers/sampleArr";
2.
3.
const arr = sampleArr(5);
4.
console.log(arr);
5.
// Expected output: an array of 5 random numbers

2. Negative Index

1.
import sampleArr from "@/helpers/sampleArr";
2.
3.
const arr = sampleArr(-5);
4.
console.log(arr);
5.
// Expected output: Error