0

I have an array containing the individual letters of a word and i want to search the array to return the index values of certain letters. However, if the word contains more a letter more than once (such as 'tree') the programme only returns one index value.

This is a sample of the code:

var chosenWord = "tree";     
var individualLetters = chosenWord.split('');
var isLetterThere = individualLetters.indexOf(e);
console.log(isLetterThere);

this code will return the number '2', as that is the first instance of the letter 'e'. How would i get it to return 2 and 3 in the integer format, so that i could use them to replace items in another array using the .splice function.

3
  • 6
    Write a function to iterate over each letter, check, and tally up results. Commented Jan 21, 2015 at 21:41
  • How do you want the indexes returned: as text, as an array, etc.? Commented Jan 21, 2015 at 21:43
  • Hint: indexOf takes a second parameter, as the position where it should start searching from. developer.mozilla.org/de/docs/Web/JavaScript/Reference/… Commented Jan 21, 2015 at 21:50

5 Answers 5

1

indexOf takes a second parameter, as the position where it should start searching from.

So my approach would be:

function findLetterPositions(text, letter) {
    var positions = new Array(),
        pos = -1;
    while ((pos = text.indexOf(letter, pos + 1)) != -1) {
        positions.push(pos);
    }
    return positions;
}

console.log(findLetterPositions("Some eerie eels in every ensemble.", "e"));

http://jsfiddle.net/h2s7hk1r/

Sign up to request clarification or add additional context in comments.

Comments

0

You could write a function like this:

function indexesOf(myWord, myLetter)
{
    var indexes = new Array();
    for(var i = 0; i < myWord.length; i++)
    {
        if(myWord.charAt(i) == myLetter)
        {
            indexes.push(i);
        }
    }
    return indexes;
}
console.log(indexesOf("tree", "e"));

2 Comments

I didn’t, but looping through every single character and comparing it yourself is rather inefficient – using already built-in functionality such as indexOf will likely perform better. (Not that is necessarily matters for typical use cases.)
That's actually not true: loops are faster than indexOf.
0

Loop through it as here:

var chosenWord = "tree";
var specifiedLetter = "e";
var individualLetters = chosenWord.split('');
var matches = [];
for(i = 0;i<individualLetters.length;i++){
  if(individualLetters[i] == specifiedLetter)
    matches[matches.length] = i; 
}

console.log(matches);

Comments

0

An alternative using string methods.

var str = "thisisasimpleinput";
var cpy = str;
var indexes = [];
var n = -1;
for (var i = cpy.indexOf('i'); i > -1; i = cpy.indexOf('i')) {
  n += i;
  n++;
  indexes.push(n);
  cpy = cpy.slice(++i);
}
alert(indexes.toString());

Comments

0
var getLetterIndexes = function(word, letter) {
  var indexes = [];
  word.split("").forEach(function(el, i) {
    el === letter && indexes.push(i);
  });
  return indexes;
};

getLetterIndexes("tree", "e"); // [2, 3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.