Start learning free

LESSON 54 OF 348 · CALCULATIONS - CHALLENGES

Your First Challenge in TypeScript: Sum of Two Numbers

Practice Read the full explanation

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:

sum(a, b) {
    // Your solution goes here
}

A method is just a function inside a class. When defining methods, we do not use the keyword function in the method's header.


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


STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS - CHALLENGES

    Your First Challenge in TypeScript, explained

    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

    1. Add the two parameters. The + operator adds numbers, so a + b is the sum.
    2. Send it back with return. return hands a value to whoever called the method. Without it the sum would be computed and thrown away, and the call would give undefined.

    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 number is a double, so 0.1 + 0.2 is 0.30000000000000004. This is normal for decimals in every language that uses doubles.
    • Negative numbers and zero work. sum(-3, 3) is 0, and sum(0, 0) is 0.

    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 just a + b; computes and discards the value.
    • Editing the class line. Keep class Solution and the method header as they are, and change only the body.
    • Using the wrong operator. a * b or a - b are 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.

    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free