Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
How to use .indexOf inside a for of loop in js

I'm a beginner to JavaScript working on my first project: Tic Tac Toe!

I'm trying to write a function that will use a for/of loop to look through all the spaces on the game board and return an array of numbers corresponding with the index of any space that has an X in it.

spaceArray is the game board, with random x's in it for now.

Here is my attempt at the function:

var s = '_'
var x = 'X'
var O = 'O'
var spaceArr = [s, s, s, x, s, s, s, s, x]

function spaceChecker(array) {
  let currGame = []
  for (var element of array) {
    if (element === 'X')
      currGame.push(array.indexOf(element))
  }
  return currGame
}
console.log(spaceChecker(spaceArr))

It currently returns:

[ 3, 3 ]

How can I get .indexOf to update each time the for/of loops?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You have used indexOf which will return the first index of the X every time you are searching for X. If you want to use for..of loop then you have to track the index at which you are on.

var s = "_";
var x = "X";
var O = "O";
var spaceArr = [s, s, s, x, s, s, s, s, x];

function spaceChecker(array) {
  let currGame = [];
  let index = 0;
  for (var element of array) {
    if (element === "X") {
      currGame.push(index);
    }
    index++;
  }
  return currGame;
}
console.log(spaceChecker(spaceArr));

ALTERNATE SOLUTION ⬇

You can just use reduce here to get the index of the X in an array

var s = "_";
var x = "X";
var O = "O";
var spaceArr = [s, s, s, x, s, s, s, s, x];

function spaceChecker(array) {
  return array.reduce((acc, curr, index) => {
    if (curr === "X") acc.push(index);
    return acc;
  }, []);
}
console.log(spaceChecker(spaceArr));

about 4 years ago · Juan Pablo Isaza Report

0

As you do not need to return anything from the loop you can use forEach loop.

forEach((element, index) => {
   // if condition met push index in custom array
});
about 4 years ago · Juan Pablo Isaza Report

0

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

So your code will always return 3 because that will be the first index where X is found on every iteration.

It's much easier to use a basic for/loop and push the index of the current iteration into currGame when X is found.

const s = '_';
const x = 'X';
const O = 'O';

const spaceArr = [s, s, s, x, s, s, s, s, x];

function spaceChecker(array) {
  const currGame = [];
  for (let i = 0; i < array.length; i++) {
    if (array[i] === 'X') currGame.push(i);
  }
  return currGame;
}

console.log(spaceChecker(spaceArr));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!