palomares
(regular)
Fri Mar 18 2005 08:55 PM
Is there the equivalent of Goto in Javascript?

Hi,

I am a complete beginner ,trying to learn some Javascript. I was asking myself if there is a command , like the Goto command in Basic. I remember:

10 bla bla
20 bla bla
30 if (Something) goto 10

Or if this is more helpful:



var number ;

number = prompt("Please enter a number"," ");

number = parseInt(number)

if (isNaN(number))
{
document.write("This is not a number<br />");
document.write(" Please try again!");

}
else

{
var result ;

result = 2*number ;

document.write("The number times two is = "+result);
}


Just after the

"Please try again",..., I'd like to make it return to the first line, therefore asking the user to add another input,...

Could anybody point me in the right direction of a command which would do this?
Many Thanks


paul_lomax
()
Sat Mar 19 2005 03:24 AM
Re: Is there the equivalent of Goto in Javascript?

'goto' is considered 'evil' in every progamming language other than Basic (very long story as to why...).. in this case you actually want to do a 'while' loop. It returns to the top of the {} as long as the condition remains true.
var number;
number = prompt("Please enter a number"," ");
number = parseInt(number)

while (isNaN(number))
{
document.write("This is not a number
");
document.write(" Please try again!");
}

var result ;
result = 2*number ;

document.write("The number times two is = "+result);




Paul Lomax
Internet Technology Manager
IPC Country and Leisure Media
- www.ipcmedia.com

Pick Me Up magazine - Real life as you've never seen it before!


Contact Us | Privacy statement Main website
Hitwise Top 10 Award Winner - Jan-Mar 2005

About us | Contact us | Link to us | Terms & Conditions | Privacy Policy
© Copyright IPC Media Limited, All rights reserved