I cant seems to get this to work.
<script language="JavaScript">
var name = null;
do
{
var name = prompt("Please enter your full name","");
}
while(name != null);
</script>
When you enter a blank string and press OK, it returns a empty string not null, null is returned only when you press cancel.
You can test for truthyness of the returned value
var name = null;
do {
name = prompt("Please enter your full name");
console.log(name)
}
while (!name);
console.log('done', name)
I would use a boolean value:
var name = false;
do
{
name = prompt("Please enter your full name",'');
}
while(!name); // While name is not true
Also you were re-declaring the name var again on within the do loop, remove the var in the loop.
var again, agreed this isn't a problem but it's still not necessary nor good practice IMO. Cheersfalse makes everything even more confusing. Now name is undefined before being initialised, then false, then either null or a string (depending on what prompt returned). I'm not talking about performance costs, but about best practises indeed.
"" != nullis true. You wantwhile (name == null)(although I'd recommendname == "")