JavaScript solution to Code Eval Problems