Last active
January 27, 2025 18:59
-
-
Save TheMuellenator/9fdb0d41d6343218ca1bcb6fb724949e to your computer and use it in GitHub Desktop.
iOS repl.it - Randomisation Challenge Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] | |
//The number of letters in alphabet equals 26 | |
var password = alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] | |
print(password) |
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
//The number of letters in alphabet equals 26 let password = alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! print(password)
The '!' on my code refers to force unwrapping optionals. Basically what optionals are saying is, "There is a value and it is [this value] or there isn't a value at all." If there wasn't a value at all, it would be 'nil'. if tried to run with randomElement alone the code would crash as will keep running and never find a solution.
Thank you for explaining this one. I am actually trying to use the alphabet.randomElement() but I was getting compiler error -- took too long to compile. Did not know you would need the '!'.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
func exercise() {
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
}