Swift code Need help with this question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Swift code Need help with this question

Write a function isIncreasing that takes a list of Ints and returns true if numbers are in increasing (equals OK) order. An empty list should result in true. * Note: Make the input label optional by including an underscore _ in front of its name func isIncreasing(_ l: [Int]) -> Bool { var flagreturn=true if l.count == 0 { flagreturn=true } else { for i: Int in l { if l[i] < l[i + 1] { flagreturn=true } else{ flagreturn=false } } } return flagreturn

18th Nov 2022, 1:06 AM
Trever Young
Trever Young - avatar
1 Answer
0
Trever Young there are a few issues. - The for loop index, i, is iterating on values stored inside l[ ] instead of incrementing as an index 0, 1, 2, 3, etc. - flagreturn is supposed to indicate true if all values are increasing, and false if any of them are not. Instead, the logic resets its value every time through the loop and in the end it represents only the last pair of numbers. Even though flagreturn might have been false in the previous interation, it will return true if the last pair is increasing. Remove the logic inside the loop that sets it to true. - Once flagreturn becomes false there is no need to continue checking. You may as well break out of the loop and return. - The function is missing a closing brace.
19th Nov 2022, 9:56 PM
Brian
Brian - avatar