Reputation: 820
let newArray = ['t', 'r', 'c', 'g', 't', 'h'];
for (let i = 0; i < newArray.length; i++) {
const x = newArray.indexOf([i])
console.log(x)
}
I'm a beginner at javascript and i cant understand why this code won't work. I am trying to produce this result in the simplest way possible:
0:4
1:-1
2:-1
3:-1
4:-1
5:-1
Instead, it is iterating over the array and producing 6 x -1. even though the first element in the array does appear again. Appreciate any help in understanding why this happens and how to fix it
Upvotes: 0
Views: 801
Reputation: 5418
From what I understood, you want to find the first following position of a repeated item in an array sequence.
Original Answer
This can be done with findIndex
, which takes a callback to determine if the index counts as found. Here you can specify that the items need to equal and the index needs to be greater than your current index, thus it is a repeated item.
let newArray = ['t', 'r', 'c', 'g', 't', 'h'];
for (let i = 0; i < newArray.length; i++) {
// Find the index of the first item which index is greater than the current item and the latter equals the item
const x = newArray
.findIndex((item, index) => index > i && newArray[i] === item);
console.log(`${i}: ${x}`);
}
Better Solution
As mentioned in the comments (thanks T.J. Crowder), indexOf
takes a second parameter as an offset to start the search at. This is the preferrable solution, as it faster and more concise.
let newArray = ['t', 'r', 'c', 'g', 't', 'h'];
for (let i = 0; i < newArray.length; i++) {
const x = newArray.indexOf(newArray[i], i+1);
console.log(`${i}: ${x}`);
}
Upvotes: 4
Reputation: 7591
The problem is that [i] in indexOf([i])
is just an array with a number inside of it.
If you had indexOf(newArray[i])
, then you would at least get the answer [0, 1, 2, 3, 0, 5] (the character will find itself, except the second t
that finds the first t
at position 0).
But I assume that you only want to know the position of the upcoming letter, and not the position of the previous ones.
What you can do is to loop from the end of the array instead, save each letter in an object with it's position and then replace it if the letter has already existed.
I will add the code in a method so it's easier to use it with other arrays.
function findOccurancies(newArray) {
let newObj = {};
let currentCharacter = '';
let x = [];
for (let i = newArray.length-1; i >= 0; i--) {
currentCharacter = newArray[i];
if (newObj.hasOwnProperty(currentCharacter)) {
x.unshift(newObj[currentCharacter]) // place the item first.
} else {
x.unshift(-1) // place the item first.
}
newObj[currentCharacter] = i;
}
return x;
}
console.log(findOccurancies(['t', 'r', 'c', 'g', 't', 'h']));
console.log(findOccurancies(['t', 'r', 'c', 'g', 't', 'h', 't']));
Upvotes: 0
Reputation: 668
I don't understand, if you just want to print the array, you need to go directly to the position:
let newArray = ['t', 'r', 'c', 'g', 't', 'h'];
for (let i = 0; i < newArray.length; i++) {
const x = newArray[i]
console.log(x)
}
And if you want to print the position of each value in the array, do it like this:
let newArray = ['t', 'r', 'c', 'g', 't', 'h'];
for (let i = 0; i < newArray.length; i++) {
const x = newArray.indexOf(newArray[i])
console.log(x)
}
Hope it will help you.
Upvotes: -3
Reputation: 69
In the code above indexOf is searching for the location of i inside the array, and isn't finding it, as none of the array elements are numbers.
Try:
const x = newArray.indexOf(newArray[i])
Upvotes: 0