Start learning free

LESSON 169 OF 677 · LOOPS & GLOBAL VARIABLES

Sum Of Numbers: Buggy Code in Scala

Practice

Inside the code editor we've tried to write a function that takes a number n as argument and returns the sum of all numbers 0 through n - 1 (0 + 1 + 2 + ... + n - 1).

So when we called computeSum(4), we expected our code to print:

6

because 0 + 1 + 2 + 3 = 6. But it seems like we made some mistakes because when we run our code, it prints:

16

Assignment:

Your task is to fix our function such that it correctly computes and returns the desired sum.

STEP 1 OF 1
    Ctrl + Enter to check