Skip to content

Instantly share code, notes, and snippets.

@chasen-bettinger
Created February 24, 2017 19:04
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 chasen-bettinger/1461e6c47ee3a0d43d94c1af27580597 to your computer and use it in GitHub Desktop.
Save chasen-bettinger/1461e6c47ee3a0d43d94c1af27580597 to your computer and use it in GitHub Desktop.
Compare the relationship between two variables
function getRelationship(x, y) {
if(x !== x) {
return "Can't compare relationships because " + x + " and undefined are not numbers";
}
if (typeof x != 'number' || typeof y != 'number') {
if(arguments.length === 2) {
if(typeof x != 'number' && typeof y != 'number') {
return "Can't compare relationships because " + x + " and " + y + " are not numbers";
}
else {
return "Can't compare relationships because " + x + " is not a number";
}
}
else if (typeof x === 'number' && typeof y != 'number') {
return "Can't compare relationships because undefined is not a number";
}
else {
return "Can't compare relationships because " + x + " and undefined are not numbers";
}
}
//Can't compare relationships because [this value] and [that value] [is]/[are] not [a] number[s].
if(x > y) {
return ">";
}
else if (x < y) {
return "<";
}
else {
return "=";
}
}
// 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