admin管理员组

文章数量:1287267

I have three numbers and I am trying to pare 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 ?

I have three numbers and I am trying to pare 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 ?

Share Improve this question asked May 8, 2019 at 14:59 DevlaDevla 3565 silver badges20 bronze badges 3
  • 3 if (jobEnd < myMonthly && myMonthly < jobStart) – ps2goat Commented May 8, 2019 at 15:02
  • Yep this helped. Will wait for the explanation why this works and that didn't. – Devla Commented May 8, 2019 at 15:04
  • 1 The < operator is binary, has two operands, the problem is that the expression is parsed as: (jobEnd < myMonthly) < jobStart, the first operand (jobEnd < myMonthly) results in a boolean value, and the subsequent operator gets a boolean and a number. Check the AST – Christian C. Salvadó Commented May 8, 2019 at 15:07
Add a ment  | 

2 Answers 2

Reset to default 10
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 )

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

if (jobEnd < myMonthly &&  myMonthly < jobStart)

本文标签: javascriptOperator lt cannot be applied to types Number and booleanStack Overflow