Rotate Arrays Left Or Right – to-rotated

Category: Javascript , Recommended | May 29, 2024
Views Total:10 views
Official Page:Go to website
Last Update:May 29, 2024


Rotate Arrays Left Or Right – to-rotated


to-rotated is a tiny JavaScript library that rotates an array of integers by a specified number of steps. If the rotated numbers move past the end, they wrap around to the beginning of the array.

How it works:

The to-rotated library provides a function toRotated to check if the input is an array and if the number of steps is a safe integer. It calculates the effective number of steps to rotate by using the modulo operation. The function then slices the array and concatenates the two segments to produce the rotated array.

How to use it:

1. Install the ‘to-rotated’ package using NPM:

$ npm install to-rotated

2. Import the library into your project:

import toRotated from ‘to-rotated’;

3. Use the toRotated function to rotate an array by a specified number of steps.

console.log(toRotated([1, 2, 3, 4, 5, 6], 3));
//=> [4, 5, 6, 1, 2, 3]
console.log(toRotated([1, 2, 3, 4, 5, 6], -1));
//=> [2, 3, 4, 5, 6, 1]

You Might Be Interested In:

Leave a Reply