In this lesson we will introduce you to how challenges work and how to make the most out of them. Let's check your first coding challenge:
Sum of Two Numbers
You are given two numbers a and b and you should return their sum.
Examples:
Input: a = 4, b = 9 Output: 13 Explanation: The sum of a and b is 4 + 9, which is 13.
This is the challenge statement. It explains what the challenge asks us to do and provides some examples to make sure we understand the statement.
Each challenge gives us some input and asks us to write some code that succesfully takes that input and uses it to produce some desired output.
In this case, the input is two numbers a and b and the output is their sum.
The class:
If you look at the code editor, you will see that everything is wrapped up inside a class:
class Solution {
}
You don't need to know anything about classes at this point, we will study them in future lessons. Just leave that code untouched.
The method:
Inside this class, we will always be provided with a method. Inside this method is where we should write our solution.
In our case the method is:
public func sum(_ a: Int, _ b: Int) -> Int {
// Your solution goes here!
}
A method is just a function inside a class. When defining methods, we use visibility keywords like public in the method's header. Don't dwell on this, we will study methods in future lessons.
Parameters and return:
This method's parameters will always match the input described in the challange's statement. In our case, the parameters are a and b, the two numbers we are given.
The method should return a value which represents the desired output described in the challange's statement. In our case, it should return a + b.
Assignment:
Follow the Coding Tutorial and let's solve our first coding challenge: Sum of Two Numbers
Free plan: 30 tutor messages. Premium is unlimited.