I bet the following situation can be more efficient by removing the part from jc.half = jc.half();. I’m just not quite sure how.
jc.half = function(){
if(jc.singlerow){
return jc.total
}else{
return jc.total / 2;
}
}
// returns function()
jc.half = jc.half();
// returns the right integer
I’m looking for a solution that does something like this:
jc.half = returnInteger(function(){
if(jc.singlerow){
return jc.total
}else{
return jc.total / 2;
}
})
// returns the right integer
The trick is that when you’ve got a function declaration that starts in such a way that the parser can tell that it’s meant to be a value in an expression, then the function definition is part of the expression, and you can use it that way and invoke the function. Really, on the right side of an assignment like that, it’s not even necessary to use the “wrapper” parentheses, but I do it just out of habit and to keep things clear to myself when reading code.
Of course in this case you could omit the function entirely.
edit — a helpful commenter notes that you mention you need an integer; if so, you’ll want to call
Math.floor()orMath.round()on your result. Or you could “cheat” a little and do this: