Solutions for Problems 1 and 2 in javascript