Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to find location of all the times a specific letter appears in string

javascript; string indexof string find

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 4ringsa6

4ringsa6

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 02 March 2016 - 05:43 PM

 

i am trying to show all the locations the letter 'e' that shows up in a string for my homework.

How do I start?

Here is the question and my attempts. some have been commented out because they did not match what i was looking for in the answer.

 

i am able to find all the times the letter e appears. Im also able to find the location of the letter 'e' but only the first occurrence of it.  

//show the location of each occurence of the character "e" in the string "beg"
// *** on one line separated by commas in the span block with id="ans6"
//
//var begE = beg.match(/e/g)
//document.getElementById("ans6").innerHTML=(beg.indexOf('e'))
// THIS METHOD COUNTS TOTAL OCCURANCE OF E COULD BE USEFUL IN NUMBER 5
var begE = 0
var positionE = 0
//for (i=0;i//positionE = (beg.indexOf('e')
while begE= (beg.indexOf('e') != -1)
positionE=begE.toString()
document.getElementById("ans6").innerHTML=(begE)