Created
December 6, 2015 20:43
-
-
Save alenabdula/d7359d8465686fb8c34b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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