solve question5 in javascript