Skip to content

Instantly share code, notes, and snippets.

@alenabdula
Created December 6, 2015 20:43
Show Gist options
  • Save alenabdula/d7359d8465686fb8c34b to your computer and use it in GitHub Desktop.
Save alenabdula/d7359d8465686fb8c34b to your computer and use it in GitHub Desktop.
function Pythagorean() {}
Pythagorean.prototype.calculate = function(obj) {
if (Object.keys(obj).length > 3 ) {
throw 'This function accepts object with maximum of 3 properties';
}
if ( typeof obj.c === 'object' ) {
return Math.sqrt(Math.pow(obj.a,2) + Math.pow(obj.b,2));
}
else {
if ( obj.c <= obj.a || obj.c <= obj.b ) {
throw 'Hypotenuse must be larger than any of the other two sides';
}
var num = obj.a || obj.b;
return Math.sqrt(Math.pow(obj.c,2) - Math.pow(num,2));
}
}
var sides = { a : 3, b : null, c : 13 }
console.log(new Pythagorean().calculate(sides));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment