Skip to content

Instantly share code, notes, and snippets.

Avatar

Frédéric Bolduc ferdbold

View GitHub Profile
@ryansmith94
ryansmith94 / triangular.js
Last active Feb 18, 2019
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.
View triangular.js
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.");
};
You can’t perform that action at this time.