JavaScript How do I implement a cycle-through array with a generator function

Today I was wondering what would be the swiftest method to provide a cycle-through array in TypeScript, as in:

['one', 'two', 'three'] 

where the next value after three would be one, and I thought that it's a good candidate for a generator function. However it does not seem to work for me. What's wrong with the following code?

function* stepGen(){
  const steps = ['one', 'two', 'three'];

  let index = 0;

  if(index < steps.length - 1){
   index++;
  } else {
   index = 0;
  }
  yield steps[index];
}

let gen = stepGen();
console.log(gen.next().value); 
console.log(gen.next().value);
console.log(gen.next().value); // should be 'three'
console.log(gen.next().value); // should be 'one'
console.log(gen.next().value);
Answer:1

I’m working on a card game in JS. The player’s cards are dynamically displayed as elements inside a div of a set width. As cards are added to the player’s hand, I’m using right: % and z-index to shift ...

I’m working on a card game in JS. The player’s cards are dynamically displayed as elements inside a div of a set width. As cards are added to the player’s hand, I’m using right: % and z-index to shift ...

I'm trying to use an example of fixed-data-table, and bundle it with webpack. I'm using babel-loader and my code otherwise bundles without issues. Running webpack points to an error which to my ...

I'm trying to use an example of fixed-data-table, and bundle it with webpack. I'm using babel-loader and my code otherwise bundles without issues. Running webpack points to an error which to my ...

I'm new to coding and I replaced some repetitive HTML with JavaScript and now the images, which used to display 4 across, are no longer displaying horizontally. They now display on the left only. I've ...

I'm new to coding and I replaced some repetitive HTML with JavaScript and now the images, which used to display 4 across, are no longer displaying horizontally. They now display on the left only. I've ...

  1. replacing html with jquery
  2. replacing html with javascript
  3. replacing html text with javascript
  4. replacing html tables with divs

How do I find vertices positions of a mesh after I used .fromGeometry(); code? I created a buffergeometry from a geomtery that I used for a mesh. Here is an example. var geom = new THREE.BoxGeometry(...

How do I find vertices positions of a mesh after I used .fromGeometry(); code? I created a buffergeometry from a geomtery that I used for a mesh. Here is an example. var geom = new THREE.BoxGeometry(...