-1

How to find all Indexes of as specified word within a lengthy string?

let word = 'Testing JavaScript, JavaScript is the Best, JavaScript is Ultimate';

Find the Indexes of word "JavaScript" from the above string in JavaScript code? Actually, its my interview question.

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
  • We're here to help you *debug* your attempts, but as it stands you haven't given us anything to debug. Please edit your question to include code, and be *more specific*. – Tyler Roper Feb 21 '19 at 17:07
  • 1
    Possible duplicate of [How to get all indexes of a pattern in a string?](https://stackoverflow.com/questions/9957827/how-to-get-all-indexes-of-a-pattern-in-a-string) and [Finding all indexes of a specified character within a string](https://stackoverflow.com/questions/10710345) and [A string method to return all placements of a specified string?](https://stackoverflow.com/questions/44729417) – adiga Feb 21 '19 at 17:08
  • Yes, actually. In my interview, they asked only this question. He gave me only 1 string as I mentioned above & asked me to write the solution. – vishnu karthik Reddy Feb 21 '19 at 17:08
  • @vishnukarthikReddy Please read [How do I ask a homework question?](https://meta.stackoverflow.com/a/334823/2026606), and specifically note the portion that says *"**Make a good faith attempt to solve the problem yourself first.** If we can't see enough work on your part, your question will likely be booed off the stage; it will be voted down and closed."* – Tyler Roper Feb 21 '19 at 17:08
  • What does not work? How is this not a duplicate: https://stackoverflow.com/questions/44729417/a-string-method-to-return-all-placements-of-a-specified-string – adiga Feb 21 '19 at 17:11

2 Answers2

1

You can use the following code.

let str = 'Testing JavaScript, JavaScript is the Best, JavaScript is Ultimate';
function findAllIndexes(string,word){
  let result = [];
  let dif = 0;
  while(true){
    let index = string.indexOf(word);
    if(index === -1) break;
    else{
      result.push(index + dif);
      let cur = string.length;
      string = string.substring(index + word.length);
      dif += cur - string.length;
    }
  }
  return result;
}
console.log(findAllIndexes(str,"JavaScript"));
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

You can do this using a regular expression.

let word = 'Testing JavaScript, JavaScript is the Best, JavaScript is Ultimate';
var regex = /JavaScript/gi,result,indices=[];
while((result=regex.exec(word)))
{
  indices.push(result.index);
}
console.log(indices);
obscure
  • 11,916
  • 2
  • 17
  • 36
  • 2
    Not to police too hard here but this question is blatantly off topic, and should not be answered. Doing so encourages this user (and others) to come back and simply ask *"Hey someone write my code for me"*, knowing that they'll get an answer. – Tyler Roper Feb 21 '19 at 17:14
  • Yes, Thanks @obscure. It worked. – vishnu karthik Reddy Feb 21 '19 at 17:14
  • @Tyler Roper - I see what you mean. It's just the 'little' helper deep inside me that just can't resist. I'll remember your words. – obscure Feb 21 '19 at 17:54
  • @obscure I hear you and often have to fight the same instinct. Your heart's in the right place! – Tyler Roper Feb 21 '19 at 18:33