Skip to content

Instantly share code, notes, and snippets.

@m3talsmith
Created May 17, 2013 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m3talsmith/5599640 to your computer and use it in GitHub Desktop.
Save m3talsmith/5599640 to your computer and use it in GitHub Desktop.
Beer Friday Challenges #1: Beer level 1 (you drink one beer before solving this) The dynamic conditional. Solve in any language, but you must solve with code - no mental substitutions. Drink, fork, and solve, people!
((15, 'less_than', 30) == true)
@markselby
Copy link

15 < 40 ? true : false

@m3talsmith
Copy link
Author

And how do you get to that state mark?

@m3talsmith
Copy link
Author

Here's my solution in ruby:

def parse_operator(operator)
  case operator.downcase.to_sym
  when :less_than
    :<
  # and so on with other operator cases
  end
end

def evaluate(key, operator, value)
  key.send(parse_operator(operator), value)
end

evaluate(10, 'less_than', 30) == true

@markselby
Copy link

JS:

var evaluations = {
  less_than: function (min, max) {
    return min < max;
  },
  more_than: function (max, min) {
    return max > min;
  }
};

function evaluate(key, operator, value) {
  return evaluations[operator](key, value);
}

console.log(evaluate(10, 'less_than', 30) == true);
console.log(evaluate(10, 'more_than', 30) == true);

Strictly, it should be === in JS although this works as intended.

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