Recap - Till 120
Part of the Fundamentals section of Coddy's Swift journey — lesson 35 of 86.
Challenge
EasyRead a person's current age as input and calculate how many years remain until they turn 120.
You will receive the following input:
- A single line containing the person's current age as a string (e.g.,
"25")
Use readLine() to read the input, safely unwrap it with if let, and convert it to an Int. Then calculate the difference between 120 and the current age.
Print: The number of years until the person turns 120
Try it yourself
// Read the person's current age as input
let input = readLine()
// TODO: Write your code below
// 1. Safely unwrap the input using if let
// 2. Convert the string to an Int
// 3. Calculate years remaining until age 120
// 4. Print the resultAll lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input