How a challenge works
Every challenge gives you some input and asks for some output. Your job is to write code that turns one into the other. This first challenge is Sum of Two Numbers: you receive two numbers, a and b, and must return their sum. For a = 4 and b = 9 the answer is 13, because 4 + 9 is 13.
The lesson's tests call your code with several pairs of numbers and compare what you return with the expected sum. So what matters is the value you return, not anything you print.
The class and the method
The editor gives you a ready-made shape to fill in:
class Solution {
sum(a: number, b: number): number {
// Write the first step below:
}
}
Two pieces are worth understanding, even though you don't need to master them yet.
The class
Everything is wrapped in class Solution. A class groups related code together. For these challenges, treat it as a container and leave its line alone. Classes get their own lessons later.
The method
Inside the class there is a method, which is a function that lives in a class. Its header differs from a plain function in one way: no function keyword. You write just the name, sum, followed by the parameters in parentheses and the body in braces.
Parameters and the return type
The parameters are the input: a and b. The annotation : number after each name says that a number is expected there. The : number after the parentheses is the return type: the method promises to give back a number. The body between the braces is where your solution goes.
Step by step
- Add the two parameters. The
+operator adds numbers, soa + bis the sum. - Send it back with
return.returnhands a value to whoever called the method. Without it the sum would be computed and thrown away, and the call would giveundefined.
The challenge needs only a single statement.
The solution in TypeScript
class Solution {
sum(a: number, b: number): number {
// Write the first step below:
return a + b;
}
}
Let's check it against the example. Calling sum(4, 9) sets a to 4 and b to 9. The expression a + b is 13, and that value is returned.
Details worth knowing
- Type annotations are for the compiler. TypeScript removes them when it turns your code into JavaScript. If a string slipped in at run time,
+would join text instead of adding numbers, for example"4" + "9"is"49". Inside the lesson the tests pass real numbers, so this doesn't come up. - Decimals follow floating-point rules. A
numberis a double, so0.1 + 0.2is0.30000000000000004. This is normal for decimals in every language that uses doubles. - Negative numbers and zero work.
sum(-3, 3)is0, andsum(0, 0)is0.
Common mistakes
- Printing instead of returning.
console.log(a + b)shows the sum but returns nothing. The challenge asks you to return it. - Forgetting
return. Writing justa + b;computes and discards the value. - Editing the class line. Keep
class Solutionand the method header as they are, and change only the body. - Using the wrong operator.
a * bora - bare valid expressions, but they give a different result from the sum.
Checking your work
Try the example in your head with other values: sum(1, 2) should give 3, sum(10, -4) should give 6, and sum(0, 0) should give 0. The editor above runs the lesson's tests for you when you validate the step.