Problem solving with javascript