range


Function: range

The range function generates an array of numbers, starting from a given start value, and ending just before a given end value, incrementing by a specified step.

Parameters:

  • start: The number from which the array starts. If end is not provided, start behaves as end and the sequence starts from zero.

  • end: The number before which the array ends (exclusive).

  • step (optional): The increment step between the numbers in the array. Default value is 1.

Returns:

  • An array of numbers starting from start (or 0 if start is used as end), up to, but not including, end, incrementing by step.

Usage

import { range } from 'reactchemy'

export default function Component() {
   const numbers = range(1, 10, 2)
   // numbers enthält [1, 3, 5, 7, 9]

   return (
     <div>
       <h1>Numbers:</h1>
       <ul>
         {numbers.map(number => (
           <li key={number}>{number}</li>
         ))}
       </ul>
     </div>
   )
}

Util

export function range(start: number, end: number, step = 1): number[] {
   const output: number[] = []
   if (typeof end === 'undefined') {
      end = start
      start = 0
   }
   for (let i = start; i < end; i += step)
      output.push(i)

   return output
}