Skip to content

Instantly share code, notes, and snippets.

View ferdbold's full-sized avatar
🎮
Still in Act 1

Frédéric Bolduc ferdbold

🎮
Still in Act 1
View GitHub Profile
@ryasmi
ryasmi / triangular.js
Last active February 18, 2019 12:46
The function triangular returns the triangular number of the inputted value. For example triangular(3) would return 6 because 1 + 2 + 3 is equal to 6. This function accounts for both negative values and zero.
var triangular = function (value) {
var abs = Math.abs(value);
return ((abs / 2) * (abs + 1)) * (abs / value) || 0;
};
// Testing code.
var testTriangular = function () {
var testTriangularValue = function (arg, value, id) {
console.log(triangular(arg) === value ? "Test " + id + " passed." : "Test " + id + " failed.");
};