Having trouble with 'while' loop in javascript

和自甴很熟 提交于 2019-12-02 09:07:26
while (wight>0);

The semicolon effectively makes that loop: while wight is greater than 0, do nothing. This forces an infinite loop, which is why the rest of your code doesn't execute.

Also, 'wight' is not the same as 'weight'. This is another error.

Furthermore, if you change that line to while (weight > 0), you will still have an infinite loop, because the code that then executes does not alter 'weight' - thus, it will always be greater than 0 (unless a number less than 0 was entered at the prompt, in which case it won't execute at all).

What you want is:

var weight;
weight=parseInt(prompt("Please, enter weight")); // Missing parenthesis
// Those two lines can be combined:
//var weight = parseInt(prompt("Please, enter weight"));

while(weight>0)
{ 
    if (weight>199 && weight<300)// REMOVE semicolon - has same effect - 'do nothing'
    {
        document.write("Tax will be" + weight*5);
        // above string probably needs to have a space at the end:
        // "Tax will be " - to avoid be5 (word smashed together with number)
        // Same applies below
    }
    else 
    {
        document.write("Tax will be" + weight*10);
    }
}

That is syntactically correct. You still need to either change the while condition, or alter 'weight' within that loop, to avoid an infinite loop.

spelling of weight :

while (wight>0);

while (weight>0);

also in

document.write("Tax will be" + wight*10);

document.write("Tax will be" + weight*10);

Try this

var weight;

weight=parseInt(prompt("Please, enter weight"));

while (weight>0)
{ 
  if (weight>199 && weight<300)
{
  document.write("Tax will be" + weight*5);
}
  else 
{
  document.write("Tax will be" + weight*10);
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!