// Math with Natural Numbers
function NMath(notNatural) {
this.NOT_NATURAL = notNatural;
}
NMath.prototype = {
ZERO_FACTORIAL: 1,
// Triangle Number
tri: function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return 0;
}
else {
return n + this.tri(n - 1);
}
},
// Factorial
fac: function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return this.ZERO_FACTORIAL;
}
else {
return n * this.fac(n - 1);
}
},
}
// Math with Whole Numbers
function ZMath() {
NMath.call(this, -666);
};
ZMath.prototype = Object.create(NMath.prototype);
// Absolute Value
ZMath.prototype.abs = function(n) {
return (n < 0) ? -n : n;
}
// Math with Rational Numbers
function QMath() {
ZMath.call(this);
}
QMath.prototype = Object.create(ZMath.prototype);
// Triangle Number
QMath.prototype.tri = function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else {
return (n*(n+1))/2;
}
}
// Quiz: What output does the following code produce?
var nMath = new NMath(-1);
var qMath = new QMath();
console.log(qMath.abs(qMath.NOT_NATURAL));
console.log(nMath.tri(5));
console.log(qMath.tri(10));
console.log(qMath.fac(3));
console.log(qMath.fac(-3));
console.log(nMath.tri(-1));
console.log(qMath.tri(-1));