Question

Am trying to validate multiple statements in Coffeescript before I continue.

I have something basic like this:

 if ext != 'jpeg' || ext != 'pdf' || ext != 'jpg'

     alert('extension must be jpg, pdf, jpeg')

What am I doing wrong here? am new to Coffee and thought something as basic as this shouldn't be hard to do.

Was it helpful?

Solution 2

You forgot to add &&.

if ext != 'jpeg' && ext != 'pdf' && ext != 'jpg'
     alert('extension must be jpg, pdf, jpeg')

OTHER TIPS

CoffeeScript has an in operator so you can say element in array to make the logic more compact:

You can use in to test for array presence, [...]

In your case:

if ext !in ['jpeg', 'pdf', 'jpg']
    alert('extension must be jpg, pdf, jpeg')

The current CoffeeScript compiler is smart enough to recognize that pattern and produces this JavaScript:

if (ext !== 'jpeg' && ext !== 'pdf' && ext !== 'jpg') {
  alert('extension must be jpg, pdf, jpeg');
}

rather than something more expensive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top