translated code from mtba homework from ruby to javascript (forgot to make commits until now)
moved variable to top of file