Write javascript functions to solve the problems.