Skip to content

#090 Triangular number

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

090 Triangular number

Create a function that takes a number as an argument.

Add up all the numbers from 1 to the number you passed to the function.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/090 Triangular number.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Create a function that takes a number as an argument.
//Add up all the numbers from 1 to the number you passed to the function.
function addUp(num) {
    return num * (num + 1) / 2;
}
import { assertEquals } from '../testHelper.js';
assertEquals(addUp(4), 10)
assertEquals(addUp(13), 91)
assertEquals(addUp(600), 180300)
assertEquals(addUp(392), 77028)
assertEquals(addUp(53), 1431)
assertEquals(addUp(897), 402753)
assertEquals(addUp(23), 276)
assertEquals(addUp(1000), 500500)
assertEquals(addUp(738), 272691)
assertEquals(addUp(100), 5050)
assertEquals(addUp(925), 428275)
assertEquals(addUp(1), 1)
assertEquals(addUp(999), 499500)
assertEquals(addUp(175), 15400)
assertEquals(addUp(111), 6216)

Clone this wiki locally