Created
January 2, 2015 12:13
-
-
Save gokhanbarisaker/567136cfb5805bfa36ae 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 getRelationship(x, y) { | |
// Your code goes here! | |
var xValid = validateNumber(x); | |
var yValid = validateNumber(y); | |
if (xValid && yValid) { | |
return getRelationshipSymbol(x, y); | |
} | |
else if(xValid) { | |
// y is invalid | |
return 'Can\'t compare relationships because ' + y + ' is not a number'; | |
} | |
else if (yValid) { | |
// x is invalid | |
return 'Can\'t compare relationships because ' + x + ' is not a number'; | |
} | |
else { | |
return 'Can\'t compare relationships because ' + x + ' and ' + y + ' are not numbers'; | |
} | |
} | |
function validateNumber(n) { | |
return (typeof n === 'number') && !isNaN(n); | |
} | |
function getRelationshipSymbol(x, y) { | |
var symbol; | |
if (x > y) { | |
symbol = '>'; | |
} | |
else if(x < y) { | |
symbol = '<'; | |
} | |
else { | |
symbol = '='; | |
} | |
return symbol; | |
} | |
// Try logging these functions to test your code! | |
console.log(getRelationship(1,4)); | |
console.log(getRelationship(1,1)); | |
console.log(getRelationship("that",2)); | |
console.log(getRelationship("this"," something else")); | |
console.log(getRelationship(3)); | |
console.log(getRelationship("hi")); | |
console.log(getRelationship(NaN)); | |
console.log(getRelationship(NaN, undefined)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UD 804, What's next, Relationships problem
References;