Home > database >  I tried this javascript code of simple if else block Error:-Declaration or statement expected
I tried this javascript code of simple if else block Error:-Declaration or statement expected

Time:06-21

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)

I tried this javascript code of simple if else block Error:-Declaration or statement expected. Why code is not working??

CodePudding user response:

Hi you are doing a mistake by putting a semi colon right after ')' bracket of your if condition. when you do this it means you are declaring an empty block for that if condition

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)

Here I corrected the mistake you made...

CodePudding user response:

it's const and not 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);
  • Related