Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range inside switch case statement in Coffeescript

I am using Handlebar in my Rails 3.2 jquery mobile application.

I am trying to write a switch case statement inside a Coffeescript method like

Handlebars.registerHelper 'status', (blog) ->
  switch(parseInt(blog.status))
    when [0..20]
      status = "active"
    when [20..40]
      status = "Moderately Active"
    when [40..60]
      status = "Very Active"
    when [60..100]
      status = "Hyper Active"
  return status

I am not getting any result . How to use range in when . Please suggest

like image 348
useranon Avatar asked Jun 26 '12 07:06

useranon


People also ask

Can we put range in switch case?

Using range in switch case in C/C++ You all are familiar with switch case in C/C++, but did you know you can use range of numbers instead of a single number or character in case statement.

Can you use greater than in switch?

When there are no matching values found, and the default argument is not specified, the SWITCH function returns a #N/A! error. SWITCH function only performs an exact match. We cannot use logical operators like greater than (>) or less than (<) in the logic used to determine a match.

Can you use greater than in switch case JavaScript?

You can use a JavaScript switch greater than the expression same as using in an if-else statement.

Which type of values switch () statement compares?

The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.


1 Answers

Your switch won't work as Cygal notes in the comments (i.e. see issue 1383). A switch is just a glorified if(a == b) construct and you need to be able to say things like:

a = [1,2,3]
switch a
...

and have it work when you switch on an array. The CoffeeScript designers thought adding a (fragile) special case to handle arrays (which is all [a..b] is) specially wasn't worth it.

You can do it with an if:

Handlebars.registerHelper 'status', (blog) ->
  status = parseInt(blog.status, 10)
  if 0 <= status <= 20
    'Active'
  else if 20 < status <= 40
    'Moderately Active'
  else if 40 < status <= 60
    'Very Active'
  else if 60 < status <= 100
    'Hyper Active'
  else
    # You need to figure out what to say here

Or with short circuiting returns like this:

Handlebars.registerHelper 'status', (blog) ->
  status = parseInt(blog.status, 10)
  return 'Something...'      if status <=   0
  return 'Active'            if status <=  20
  return 'Moderately Active' if status <=  40
  return 'Very Active'       if status <=  60
  return 'Hyper Active'      if status <= 100
  return 'Something else'    # This return isn't necessary but I like the symmetry

Note that you have three special cases that you need to add strings for:

  1. status < 0.
  2. status > 100.
  3. status is NaN. This case would usually fall under the final "it isn't less than or equal to 100" branch since NaN => n and NaN <= n are both false for all n.

Yes, you're absolutely certain that the status will always fall within the assumed range. On the other hand, the impossible happens all the time software (hence the comp.risks mailing list) and there's no good reason to leave holes that are so easily filled.

Also note the addition of the radix argument to the parseInt call, you wouldn't want a leading zero to make a mess of things. Yes, the radix argument is optional but it really shouldn't be and your fingers should automatically add the , 10 to every parseInt call you make.

like image 174
mu is too short Avatar answered Oct 04 '22 01:10

mu is too short