General Q: How to show all location of certain letter in string?

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.

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<beg.length;i++){
//positionE = (beg.indexOf(‘e’)
while begE= (beg.indexOf(‘e’) != -1)
positionE=begE.toString()
document.getElementById(“ans6”).innerHTML=(begE)

@cssplayer42079,

var text = “eeennennennnenne”;
var charFound=;
var beginCnt = 0;
var indxNr;
var searching = true;

while (searching) {
indxNr = text.substring(beginCnt).indexOf(‘e’);
if (indxNr != -1 ) {
charFound.push(beginCnt+indxNr);
}
else {
searching = false;
charFound = charFound.join();
}
console.log(beginCnt, indxNr ,text.substring(beginCnt) );
beginCnt += indxNr+1;
}
console.log( charFound);

[output]
0 0 'eeennennennnenne'
1 0 'eennennennnenne'
2 0 'ennennennnenne'
3 2 'nnennennnenne'
6 2 'nnennnenne'
9 3 'nnnenne'
13 2 'nne'
16 -1 ''
0,1,2,5,8,12,15
[end]
1 Like

@cssplayer42079,

var text = "eeennennennnenne";
var charFound=[];
var searching = true;

while (searching) {
    for ( var i=0, len =text.length; i < len ; i++) {
         if ( text[i] === 'e') {
             charFound.push(i);
         }
     }
     searching = false;
     charFound = charFound.join();
}
console.log( charFound);
1 Like

you sir are a genius! thanks a bunch!

why does text have the array i around it? text[i]
and what does .join do?

@cssplayer42079,
google search
== the Book ==
[your question] site:developer.mozilla.org
array join() site:developer.mozilla.org


1 Like

thank you. That website will be very helpful to me! thanks!