This is type of Greedy approach, because we are checking the max val. Convert this java code to Javascript,