1
\$\begingroup\$

I have such cases:

stock status can be in stock, undefined (there is no such field in the object) or any other text

stock status is true if it is 'in stock' or undefined all others are false

so this is my test object

obj = 
    one:
        stock: 'in stock'
    two:
        some: 'x'
    three:
        stock: 'not in stock blah blah any other text'

my method

stockCheck = (stock, msg) ->
    console.log {msg}
    return true if !stock or stock == 'in stock'

and some simply test

console.log stockCheck(obj.one.stock)?, 'should be in stock'
console.log stockCheck(obj.two.stock)?, 'should be in stock'
console.log stockCheck(obj.three.stock)?, 'should not be in stock'

is my code well for such cases?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

There are issues with your test function, don't return true, just return the result of the test you're doing or your function won't return anything if the test is false:

stockCheck = (stock, msg) ->
    console.log {msg}
    return !stock or stock is 'in stock'

Also, why {msg}? Shouldn't it be just console.log msg?

Your console.log tests are erronous too, your function is supposed to do the test not you when you call it, remove the ? from these lines:

console.log stockCheck(obj.one.stock) # true
console.log stockCheck(obj.two.stock) # true
console.log stockCheck(obj.three.stock) # false
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.