Skip to content

Instantly share code, notes, and snippets.

@gokhanbarisaker
Created January 2, 2015 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokhanbarisaker/567136cfb5805bfa36ae to your computer and use it in GitHub Desktop.
Save gokhanbarisaker/567136cfb5805bfa36ae to your computer and use it in GitHub Desktop.
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));
@gokhanbarisaker
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment