Coin Algorithm using JavaScript