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
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.
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.
You can use a JavaScript switch greater than the expression same as using in an if-else statement.
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.
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 return
s 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:
status < 0
.status > 100
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With