Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
New to JS and trying to understand this function while loop
Post Body

I recently started learning more and more about Javascript and have found some practice questions/scenarios and I sometimes google stuff on how to approach it or learn new commands, parameters, etc through the searches

I'm currently working on finding all instances of "waldo" within a string that I've set to lowercase. During my searches I found this and kind of want to rubberduck to see if my understanding is correct.

'a' is set as an empty array while the index value is set to -1 so it looks at the end of the string.

while the index value is equal to the index value of the substring location, it will add 1 to the index, as long as the index position is >=0.

It will push the accepted positions into 'a' and then return a'

function locations(substring,string){   var a=[],i=-1;   while((i=string.indexOf(substring,i 1)) >= 0) a.push(i);   return a; }  console.log(locations("s","scissors")); //-> [0, 3, 4, 7]

Author
Account Strength
100%
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
9,114
Link Karma
553
Comment Karma
8,165
Profile updated: 2 days ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago