constant bill = 275;
let tip = 0;
let total = 0;
if (bill > 50 && bill < 300);
{
tip = 0.15 * bill;
console.log("tip", tip, "bill", bill)
}
else if (bill < 50 || bill > 300);
{
tip = 0.20 * bill;
console.log("tip", tip, "bill", bill)
}
else
{
console.log("No tip")
}
total = bill tip;
console.log("total", total)
我嘗試了這個簡單的 JavaScript 代碼 if else block Error:-Declaration or statement expected。為什么代碼不起作用?
uj5u.com熱心網友回復:
它是const而不是constant
const bill = 275;
let tip = 0;
let total = 0;
if (bill > 50 && bill < 300) {
tip = 0.15 * bill;
console.log("tip", tip, "bill", bill);
} else if (bill < 50 || bill > 300) {
tip = 0.2 * bill;
console.log("tip", tip, "bill", bill);
} else {
console.log("No tip");
}
total = bill tip;
console.log("total", total);
uj5u.com熱心網友回復:
嗨,您在 if 條件的 ')' 括號后面加上一個分號,這是一個錯誤。當你這樣做時,這意味著你正在為 if 條件宣告一個空塊
constant bill = 275;
let tip = 0;
let total = 0;
if (bill > 50 && bill < 300) {
tip = 0.15 * bill;
console.log("tip", tip, "bill", bill)
}else if (bill < 50 || bill > 300) {
tip = 0.20 * bill;
console.log("tip", tip, "bill", bill)
}else {
console.log("No tip")
}
total = bill tip;
console.log("total", total)
在這里,我糾正了你犯的錯誤......
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/496725.html
標籤:javascript if 语句 编码风格
