While Loop Structure Javascript Substring Indexof