首页 > 解决方案 > 运算符 < 不能应用于 Number 和 boolean 类型

问题描述

我有三个数字,我正在尝试比较其中一个是否包含在其他 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'

  }

为什么我收到此错误?我应该改变什么?

标签: javascripttypescript

解决方案


jobEnd < myMonthly

将评估为真或假。所以如果你写:

if( jobEnd < myMonthly < jobStart)

它基本上试图评估

if( true < jobStart )

< 运算符应用于布尔值,因为 jobStart 是一个数字。

你需要这样写:

if( jobEnd < myMonthly && myMonthly < jobStart )

推荐阅读