JavaScript exercise (Math, arrays, loops, functions)