Post

Replies

Boosts

Views

Activity

Reply to Loop over two arrays with different length
The loop will finish when arrayOfItems is finished. But it is a dynamic array in the sense that sometimes n could be 100 or 1000. I think I could approach the problem like this but it is not efficient and is difficult to read: In this example, assume arrayOfItems is an array of String items not Integers, so in the loop I will use .indices to obtain the index. swift import Foundation let arrayOfItems = (0...100) let arrayOfFive = ["Item1", "Item2", "Item3", "Item4", "Item5"] for index in arrayOfItems {     if (0...4).contains(index) {         print(index)         print(arrayOfFive[index])     } else if (5...9).contains(index) {         print(index)         print(arrayOfFive[index - 5])     } else if (10...19).contains(index) {         if (10...14).contains(index) {             print(index)             print(arrayOfFive[index - 10])         } else {             print(index)             print(arrayOfFive[index - 15])         }     } } // Using this approach I will have to create if statements every tens. So, it's not generic for any n numbers
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21