While Loop Structure Javascript Substring Method