javascript - while loop not working correctly with the > token -
this not seem working not sure how while loop work correctly appreciated.
function getproductcode() { productcode = parseint(prompt("enter product code: ")); while (productcode < 1 || > 9999) { document.writeln("error! product code must between 1 - 9999"); parseint(prompt("enter product code: ")); } return productcode } getproductcode()
you're missing operand (productcode) @ left side:
while (productcode < 1 || productcode > 9999) ^^^^^^^^^^^ and:
- supply radix
parseint. when unspecified,010becomes 8 (octal literal). - do not leak variables global scope, use
vardeclare local variables. - invert logic, or use
isnan. when invalid number supplied (nan), loop should not stop. - it's better move message
document.writelndialog. - assign new value
productcode. otherwise, won't far... - important: dialogs can disabled in browsers. not loop infinitely many times, add treshold.
final code takes care of first 5 bullet points:
function getproductcode() { var productcode = parseint(prompt("enter product code: "), 10); while (!(productcode >= 1 && productcode <= 9999)) { productcode = parseint(prompt("error! product code must between 1 - 9999\nenter product code: "), 10); } return productcode; } i have not implemented treshold, it's that.
Comments
Post a Comment