Raised an error in if-else and made while depend on sample.length