如果我在输入字段中输入100,则第149行开始的if语句返回TRUE

(编辑)我看到StackOverflow代码块中没有行号,所以这是我正在谈论的if语句:

if (change > drawer && !set) {
    console.log("Claims that",change,">",drawer);
    set = true;
    cond = 4; //insufficient funds in drawer
}

我判断零钱和抽屉的价值.(抽屉=335.41.Change=96.74)我的控制台日志(log)都证实了这一点.如果更改大于抽屉,第149行的if语句应该为FALSE,但其计算结果为TRUE,为什么?335.41不可能也不可能低于96.74.我在这条if语句的前面、里面和后面都放了日志(log),任何时候都没有定义这些值,那么有什么问题呢?以下是我的script.js代码:

let price = 3.26;
let cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100]
];
let cash;
let messages = ["Status: OPEN", "Status: CLOSED", "Status: INSUFFICIENT_FUNDS"];
let cond;
let change;
let drawer;
let str = "";
let numberOfUnits = 0;
let multiplier = Float32Array.from([0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100]);
let purchaseBtn = document.getElementById("purchase-btn");
let changeDue = document.getElementById("change-due");
let itemPrice = document.getElementById("item-price");
let denominant = document.getElementById("change");
let clearBtn = document.getElementById("clear-btn");
let unit = Int32Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0]);
let numIn = document.getElementById("cash");
let ap = document.getElementById("amountP");
let an = document.getElementById("amountN");
let ad = document.getElementById("amountD");
let aq = document.getElementById("amountQ");
let ar = document.getElementById("amountR");
let af = document.getElementById("amountF");
let ae = document.getElementById("amountE");
let at = document.getElementById("amountT");
let ah = document.getElementById("amountH");
let hp = document.getElementById("haveP");
let hn = document.getElementById("haveN");
let hd = document.getElementById("haveD");
let hq = document.getElementById("haveQ");
let hr = document.getElementById("haveR");
let hf = document.getElementById("haveF");
let he = document.getElementById("haveE");
let ht = document.getElementById("haveT");
let hh = document.getElementById("haveH");
let gp = document.getElementById("giveP");
let gn = document.getElementById("giveN");
let gd = document.getElementById("giveD");
let gq = document.getElementById("giveQ");
let gr = document.getElementById("giveR");
let gf = document.getElementById("giveF");
let ge = document.getElementById("giveE");
let gt = document.getElementById("giveT");
let gh = document.getElementById("giveH");
changeDue.setAttribute("readonly", true);
itemPrice.setAttribute("readonly", true);

document.addEventListener('DOMContentLoaded', () => {
  populator();
});

function populator() {
  gp.innerHTML = unit[0];
  gn.innerHTML = unit[1];
  gd.innerHTML = unit[2];
  gq.innerHTML = unit[3];
  gr.innerHTML = unit[4];
  gf.innerHTML = unit[5];
  ge.innerHTML = unit[6];
  gt.innerHTML = unit[7];
  gh.innerHTML = unit[8];
  ap.innerHTML = "$ " + cid[0][1].toFixed(2);
  an.innerHTML = "$ " + cid[1][1].toFixed(2);
  ad.innerHTML = "$ " + cid[2][1].toFixed(2);
  aq.innerHTML = "$ " + cid[3][1].toFixed(2);
  ar.innerHTML = "$ " + cid[4][1].toFixed(2);
  af.innerHTML = "$ " + cid[5][1].toFixed(2);
  ae.innerHTML = "$ " + cid[6][1].toFixed(2);
  at.innerHTML = "$ " + cid[7][1].toFixed(2);
  ah.innerHTML = "$ " + cid[8][1].toFixed(2);
  hp.innerHTML = Math.floor(cid[0][1] / multiplier[0]);
  hn.innerHTML = Math.floor(cid[1][1] / multiplier[1]);
  hd.innerHTML = Math.floor(cid[2][1] / multiplier[2]);
  hq.innerHTML = Math.floor(cid[3][1] / multiplier[3]);
  hr.innerHTML = Math.floor(cid[4][1] / multiplier[4]);
  hf.innerHTML = Math.floor(cid[5][1] / multiplier[5]);
  he.innerHTML = Math.floor(cid[6][1] / multiplier[6]);
  ht.innerHTML = Math.floor(cid[7][1] / multiplier[7]);
  hh.innerHTML = Math.floor(cid[8][1] / multiplier[8]);
  itemPrice.value = price;
  drawer = (Number(cid[0][1]) + Number(cid[1][1]) + Number(cid[2][1]) + Number(cid[3][1]) + Number(cid[4][1]) + Number(cid[5][1]) + Number(cid[6][1]) + Number(cid[7][1]) + Number(cid[8][1])).toFixed(2);
}

function moneyIn(cashDistributor) {
  for (let i = 0; i < 9; i++) {
    while (cashDistributor >= Number(multiplier[8 - i])) {
      cid[8 - i][1] = cid[8 - i][1] + Number(multiplier[8 - i].toFixed(2));
      cashDistributor -= Number(multiplier[8 - i]);
    }
  }
  populator();
}

function dividender() {
  for (let i = 8; i > 0; i--) {
    numberOfUnits = Math.floor(cid[i][1] % change) / Number(multiplier[i]);
    console.log(cid[i][0], ": ", cid[i][1], ", Units: ", numberOfUnits);
    if (numberOfUnits > 0) {
      unit[i] = numberOfUnits;
      cid[i][1] -= (unit[i] * Number(multiplier[i]));
      change -= (unit[i] * Number(multiplier[i]));
      let hold = (unit[i] * Number(multiplier[i])).toFixed(2);
      str += (cid[i][0] + ": $" + hold + " ");
    }
  }
  if (change < 0.01) {
    cond = 0;
  } else cond = 2;
}

function update() {
  if (cond < 4) {
    moneyIn(cash);
    denominant.style.display = "inline";
    denominant.style.border = "1px solid #ad2";
    denominant.style.width = "346px";
    denominant.style.overflow = "hidden";
  }
  if (cond < 2) {
    changeDue.innerHTML = messages[cond] + " " + str;
  }
  if (cond === 2) {
    changeDue.innerHTML = messages[cond];
  }
  if (cond === 3) {
    changeDue.innerHTML = "No change due - customer paid with exact cash";
  }
  if (cond === 4) {
    changeDue.innerHTML = messages[2] + " " + str;
  }
  if (cond === 5) {
    changeDue.innerHTML = "Please enter a valid number";
  }
  if (cond === 6) {
    alert("Customer does not have enough money to purchase the item");
  }
}

purchaseBtn.addEventListener("click", () => {
  str = "";
  unit = Int32Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0]);
  cash = (parseFloat(numIn.value == undefined ? "" : numIn.value.trim())).toFixed(2);
  change = (cash - price).toFixed(2);
  console.log("change:", change, ", drawer:", drawer);
  let set = false;
  if (!cash && !set) {
    set = true;
    cond = 5; //no value entered
  }
  if (cash < price && !set) {
    set = true;
    cond = 6; //customer doesn't have enough money
  }
  if (change > drawer && !set) {
    console.log("Claims that", change, ">", drawer);
    set = true;
    cond = 4; //insufficient funds in drawer
  }
  if (cash == price && !set) {
    set = true;
    cond = 3; //paid with exact amount
  }
  if (change == drawer && !set) {
    set = true;
    cond = 1; //register closed with a ballance of 0
    dividender();
  }
  if (change < drawer && !set) { //Normal transaction
    set = true;
    dividender();
  }
  update(cond);
  populator();
});

clearBtn.addEventListener("click", () => {
  changeDue.innerHTML = "";
  itemPrice.value = price;
  cash = 0;
  numIn.value = "";
  str = "";
  unit = Int32Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0]);
  numberOfUnits = 0;
  denominant.style.border = "none";
  denominant.style.display = "none";
});
* {
  font-size: 20px;
}

html,
body {
  height: 130%;
  width: 100%;
  border: 1px solid rgb(221, 218, 34);
  background-color: rgb(129, 64, 3);
}

body {
  color: rgb(55, 248, 184);
  flex-direction: column;
  justify-content: center;
  align-items: baseline;
}

td {
  border: 1px solid #ad2;
}

tr {
  border: 1px solid #ad2;
}

th {
  border: 1px solid #ad2;
}

#th1 {
  background-color: rgb(38, 92, 0);
  color: rgb(55, 248, 184);
  width: 80px;
  height: 80px;
  border-radius: 50%;
  outline-style: dotted;
  outline-width: 2px;
  outline-color: rgb(255, 215, 0);
}

p {
  color: rgb(55, 248, 184);
  width: 175;
  margin: auto;
}

.child {
  display: flex;
  align-items: center;
  margin-left: 9;
}

.child p {
  margin: 10;
}

#change {
  border: 1px solid #ad2;
  width: 346px;
  padding-right: 250px;
  display: none;
  overflow: hidden;
}

#change-table {
  border: 1px solid #ad2;
  text-align: center;
}

#status {
  border: 1px solid #ad2;
  width: 246px;
  float: left;
  overflow: hidden;
}

#statulayui-table {
  border: 1px solid #ad2;
  text-align: center;
}

#purchase-btn {
  border: 1px solid #ad2;
  background-color: orange;
  color: rgb(23, 95, 71);
  width: 112px;
  margin: 10px;
}

#clear-btn {
  border: 1px solid #ad2;
  background-color: orange;
  color: rgb(23, 95, 71);
  width: 112px;
  margin: 10px;
}

#change-due {
  background-color: rgb(160, 111, 54);
  color: rgb(55, 248, 184);
  margin-left: 10px;
}

#change-due:focus {
  background-color: rgb(165, 134, 99);
  color: rgb(55, 248, 184);
}

#cash {
  border: 1px solid #ad2;
  background-color: rgb(160, 111, 54);
  color: rgb(55, 248, 184);
  width: 260px;
  margin-left: 10px;
}

#cash:focus {
  border: 1px solid #ad2;
  background-color: rgb(165, 134, 99);
  color: rgb(55, 248, 184);
}

#item-price {
  border: 1px solid #ad2;
  background-color: rgb(160, 111, 54);
  color: rgb(55, 248, 184);
  width: 100px;
  margin-left: 10px;
}

#item-price:focus {
  border: 1px solid #ad2;
  background-color: rgb(165, 134, 99);
  color: rgb(55, 248, 184);
}
<h1>
  <p title="Don't shortchange yourself or the customer">Cash register</p>
</h1>
<div class="parent">
  <div class="child">Item Price: $
    <input type="input" id="item-price" value="1.87">
    <button id="purchase-btn">Buy Item</button>
  </div>
  <br>
  <div class="child">&nbsp;&nbsp;&nbsp;Amount tendered:
    <input type="input" id="cash">
  </div>
  <br>
  <div class="child">
    &nbsp;&nbsp;&nbsp;Customer's change:
    <div id="change-due"></div>
  </div>
  <br>
  <div class="child">
    <button id="clear-btn">CLEAR ALL</button>
  </div>
  <div id="change" class="child">
    <table id="change-table">
      <tr>
        <th>Currency Unit</th>
        <th>Give customer</th>
        <th>Currency Unit</th>
        <th>Give customer</th>
      </tr>
      <tr>
        <td id="th1">Penny</td>
        <td id="giveP">0</td>
        <td id="th1">Five Dollars</td>
        <td id="giveF">0</td>
      </tr>
      <tr>
        <td id="th1">Nickle</td>
        <td id="giveN">0</td>
        <td id="th1">Ten Dollars</td>
        <td id="giveE">0</td>
      </tr>
      <tr>
        <td id="th1">Dime</td>
        <td id="giveD">0</td>
        <td id="th1">Twenty Dollars</td>
        <td id="giveT">0</td>
      </tr>
      <tr>
        <td id="th1">Quarter</td>
        <td id="giveQ">0</td>
        <td id="th1">One Hundred Dollars</td>
        <td id="giveH">0</td>
      </tr>
      <tr>
        <td id="th1">Dollar</td>
        <td id="giveR">0</td>
        <td id="th1">&nbsp;</td>
        <td id="give">&nbsp;</td>
      </tr>
    </table>
  </div>
  <br>
  <div id="status">
    <table id="statulayui-table">
      <tr>
        <th>Currency Unit</th>
        <th>$$$ Value</th>
        <th>Count</th>
      </tr>
      <tr>
        <td>Penny</td>
        <td id="amountP">$1.01</td>
        <td id="haveP">101</td>
      </tr>
      <tr>
        <td>Nickle</td>
        <td id="amountN">$2.05</td>
        <td id="haveN">41</td>
      </tr>
      <tr>
        <td>Dime</td>
        <td id="amountD">$3.10</td>
        <td id="haveD">31</td>
      </tr>
      <tr>
        <td>Quarter</td>
        <td id="amountQ">$4.25</td>
        <td id="haveQ">17</td>
      </tr>
      <tr>
        <td>Dollar</td>
        <td id="amountR">$90.00</td>
        <td id="haveR">90</td>
      </tr>
      <tr>
        <td>Five Dollars</td>
        <td id="amountF">$55.00</td>
        <td id="haveF">11</td>
      </tr>
      <tr>
        <td>Ten Dollars</td>
        <td id="amountE">$20</td>
        <td id="haveE">2</td>
      </tr>
      <tr>
        <td>Twenty Dollars</td>
        <td id="amountT">$60</td>
        <td id="haveT">3</td>
      </tr>
      <tr>
        <td>One Hundred Dollars</td>
        <td id="amountH">$100</td>
        <td id="haveH">1</td>
      </tr>
    </table>
  </div>

推荐答案

可能是字符串比较:

console.log(335.41 > 96.74)
console.log('335.41' > '96.74')
console.log(Number('335.41') > Number('96.74'))

Javascript相关问答推荐

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

没有输出到带有chrome.Devtools扩展的控制台

使用下表中所示的值初始化一个二维数组

获取Uint8ClampedArray中像素数组的宽度/高度

为什么useState触发具有相同值的呈现

更改预请求脚本中重用的JSON主体变量- Postman

以Angular 实现ng-Circle-Progress时出错:模块没有导出的成员

如何在Angular拖放组件中同步数组?

Use Location位置成员在HashRouter内为空

使用auth.js保护API路由的Next.JS,FETCH()不起作用

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

获取';无法解决导入和导入";slick-carousel/slick/slick-theme.css";';错误

为什么云存储中的文件不能公开使用?

删除元素属性或样式属性的首选方法

AG-GRIDreact 显示布尔值而不是复选框

用Reaction-RT-Chart创建实时条形图

在Press Reaction本机和EXPO av上播放单个文件

ReactJS在类组件中更新上下文

我如何才能让p5.js在不使用实例模式的情况下工作?

我如何让我的弹力球在JavaScript和HTML画布中相互碰撞后改变 colored颜色 ?