arrays - Creating a for-in loop that prints out players name and score in Swift -
this question has answer here:
- iterate on 2 arrays simultaneously 5 answers
var players = ["donna", "craig", "brian", "anna"] let scores = [2, 2, 4, 5] (indexx, score) in scores.enumerate() { (index, player) in players.enumerate() { if indexx != index { continue } print("player's name: \(player) score: \(score)") } }
i made method using have learned far for-in loops , arrays. there more sufficient way of creating for-in loop prints out players name , score?
player's name: donna score: 2 player's name: craig score: 2 player's name: brian score: 4 player's name: anna score: 5
given 2 arrays
var players = ["donna", "craig", "brian", "anna"] let scores = [2, 2, 4, 5]
you can write
zip(players, scores).foreach { print("player's name: \($0.0) score: \($0.1)") }
and output
player's name: donna score: 2 player's name: craig score: 2 player's name: brian score: 4 player's name: anna score: 5
Comments
Post a Comment