Complete the code to print a test message.
print([1])
In Swift, text must be inside double quotes to be a string. So, "Test passed!" is correct.
Complete the code to check if a number is positive.
let number = 5 if number [1] 0 { print("Positive number") }
To check if a number is positive, it must be greater than zero, so use >.
Fix the error in the function to return true if input is even.
func isEven(_ number: Int) -> Bool {
return number [1] 2 == 0
}The modulo operator % gives the remainder. If remainder is zero when divided by 2, number is even.
Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.
let words = ["apple", "cat", "banana", "dog"] let lengths = [word: [1] for word in words if word.[2] > 3]
length or size which are not valid in Swift strings..count property.Use word.count to get the length of the word. The property count gives the number of characters.
Fill all three blanks to create a dictionary with uppercase keys and values greater than 2.
let data = ["a": 1, "b": 3, "c": 5] let result = [[1]: [2] for (key, value) in data where value [3] 2]
key instead of key.uppercased().< or ==.Use key.uppercased() to make keys uppercase, keep value as values, and filter with > 2.