![](/img/trans.png)
[英]I can't access a css style with my javascript code. what is wrong with my code?
[英]Am getting my "t" as undefined in the console and i do not know what is wrong with my code. How can I define my 't' in this code
我在課程中遇到了編碼挑戰(javascript 教程),這是我的替代方案,但我沒能弄明白,所以請幫助我。
// alternatine
const bill = [22, 295, 176, 440, 37, 105, 10, 1100, 86, 52];
const tipstxt = [];
const totalstxt = [];
// const tiptxt = cash
// const tip = (tiptxt);
for (let x = 0; x < bill.length; x++) {
if (bill[x] >= 50 && bill[x] <= 300) t = 0.15 * bill[x];
if (bill[x] <= 50 && bill[x] >= 300) t = 0.2 * bill[x];
tipstxt.push(t);
}
console.log(tipstxt);
我預計如果一個值在 50 到 300 之間,那么 t=0.15 bill[x] 如果不是 t=0.2 bill[x]
您的第二個 if 語句將始終評估為 false,因為您的第二個 if 條件中的表達式是矛盾的。
if (bill[x] <= 50 && bill[x] >= 300)
如果 bill[x] 小於或等於 50,則 bill[x] <= 50 為真,而
如果 bill[x] 大於或等於 300,bill[x] >= 300 將為真。
由於一個數字不能同時小於 50 和大於 300,因此此 if 條件的計算結果始終為 false。 因此,如果您的第一個 if 語句的計算結果也為假,則 t 的值仍未定義。 因此,錯誤。
第二個 if 語句的正確實現是:
if (bill[x] <= 50 || bill[x] >= 300)
如果 bill[x]小於或等於 50或大於或等於 100,則可以計算 t can。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.