This is the following code I have
val idNumber: String = "1342342"
var randomNumber = 0
for (number in idNumber) {
if (randomNumber < 20) {
// Do something
randomNumber++
}
}
Here I've a for loop which iterates through the idNumber and at the same time I've to check if randomNumber is less than 20.
In Java, I can write this as
for (int i = 0 ; i < idNumber.length() && randomNumber < 20 ; i++) {
}
How do I do the same in Kotlin?
I went through Stackoverflow for this problem and found this solution which explains to use a more functional approach. Please suggest me a good solution for the problem I mentioned above.
Edit 1: I understand that while loop can be used to do a logical and, but I would like to know what's the functional way of solving this.
Edit 2: Added answer, but still would love to hear from someone who can give a better insight to this problem