Devla
Devla

Reputation: 356

Operator < cannot be applied to types Number and boolean

I have three numbers and I am trying to compare if one of them includes in other 2.

  let myMonthly = this.profile.expectedpayhourlystart + 
  this.profile.expectedpayhourlyend;
  myMonthly = ((((myMonthly/2)*8)*5)*4);
  let jobStart = item.monthlyPrice - 200;
  let jobEnd = item.monthlyPrice + 200;

  if( jobEnd < myMonthly < jobStart ){ <-- 'Operator < cannot be applied to boolean or number'

  }

Why I am getting this error ? What should I change ?

Upvotes: 5

Views: 7949

Answers (2)

obscure
obscure

Reputation: 12891

jobEnd < myMonthly

will evaluate to true or false. So if you write:

if( jobEnd < myMonthly < jobStart)

it essentially tries to evaluate

if( true < jobStart )

which is applying a < operator to boolean because jobStart is a number.

You need to write it like this:

if( jobEnd < myMonthly && myMonthly < jobStart )

Upvotes: 10

user95227
user95227

Reputation: 1953

If you stack the < operator like that it will evaluate jobEnd < myMonthly which results in a boolean and compare the result to jobStart which is a number. Use && like

if (jobEnd < myMonthly &&  myMonthly < jobStart)

Upvotes: 0

Related Questions