reformat js code

code tidier than before with the use of more loops, unused functions also deleted from code. More efficient program