sampleArr
Generates an array of random numbers.
1. Code
/**
* Generates an array of random numbers.
*
* @param size - The size of the array to generate.
* @returns An array of random numbers.
* @throws {Error} If the size is a negative number.
*/
const sampleArr = (size: number) => {
if (size < 0) throw new Error("Size must be a positive number");
return Array.from({ length: size }, (_, i) => Math.floor(Math.random() * i));
};
export default sampleArr;
2. Installation
npx @jrtilak/lazykit@latest add sampleArr -ts
3. Description
This sampleArr function is a utility function in JavaScript that takes a size parameter and generates an array of random numbers. The size parameter specifies the length of the array to generate. The function uses the Math.random() method to generate random numbers between 0 and the specified size. The function returns an array of random numbers with the specified length.
The function throws an error if the size parameter is a negative number.
4. Props
Prop
Type
Default Value
size*number---
5. Examples
import sampleArr from ".";
const arr = sampleArr(5);
console.log(arr);
// Expected output: an array of 5 random numbers
// const negativeArr = sampleArr(-1);
// Expected output: Throws an error