Skip to content

Instantly share code, notes, and snippets.

@gorork
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorork/b18c07205cf862291a64 to your computer and use it in GitHub Desktop.
Save gorork/b18c07205cf862291a64 to your computer and use it in GitHub Desktop.
P2: JavaScript Challenge #1
var getRelationship = function(x, y) {
// If both arguments are numbers
if ( !isNaN(x) && !isNaN(y) ) {
if ( x == y ) { // if they are equal, return '='
return '=';
} else { // if not, return comparison operator
return x < y ? '<' : '>';
}
// Return error if both arguments are not numbers
} else if ( isNaN(x) && isNaN(y) ) {
return errorMsg + x + ' and ' + y + ' are not numbers';
// Return error if one of arguments is not a number
} else {
return isNaN(x) ? errorMsgFor(x) : errorMsgFor(y);
}
};
// Error message base
var errorMsg = 'Can\'t compare relationships because ';
// Return error message for one argument
var errorMsgFor = function(invalidArg) {
return errorMsg + invalidArg + ' is not a number';
};
// 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