angus-c / angus-c/just

[just-rotate-right] Rotate array elements to the right

Aperta
#537 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
JavaScript
Stelle
6.2k
Fork
209
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

It would be helpful to introduce a method to rotate array elements. Something this simple would do the trick:

### Function definition
```ts
function rotateRight(arr: T[], k: number): T[] {
const mod = k % arr.length;
return arr.slice(-mod).concat(arr.slice(0, -mod));
}
```

### Usage
```ts
rotateRight([1, 2, 3], 1) // -> [3, 1, 2]
rotateRight([1, 2, 3], -1) // -> [2, 3, 1]
rotateRight([1, 2, 3], 2) // -> [2, 3, 1]
```

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.