ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wrote a program in JavaScript, embedded in a web-page, to process a series of numbers. The program should terminate when the startNum parameter fed to the function which outputs the results reaches 9999. However, this is not happening. The relevant bit of code (from the page body) is:
Code:
<script type="text/JavaScript">
function outputResult(startNum,Iterations,finNum,functions)
{
if (startNum != 9999)
{
//several document.writeln statements snipped...
generateNumber(startNum)
}
else
{
alert("this should be the end")
flag = 0
}
}
if (flag == 1)
{
var startValue = prompt("Enter starting number.")
//irrelevant error checking snipped...
generateNumber(startValue)
}
</script>
generateNumber() is the function (located in the page head) which starts a new pass through the program. The block beginning with if (flag == 1) initiates the program.
The problem is that, although the else... block of the outputResult() function is executed (the alert box I inserted, in an attempt to debug it, does appear), the program then continues. I cannot see why this happens.
Is there something fundamental that I'm not understanding about JavaScript? Do I need to add an explicit command to exit the program?
I did declare flag outside the function, however (though in the script block in the page head), which I thought made it a global variable . Also, the bottom if block doesn't seem to be implicated, as the prompt box doesn't appear (possibly should have said that earlier).
It is called from a function (actually, one of two functions) located in the page head. It is definitely being called correctly, as otherwise there would be no output, and the else block of outputResult does get called when 9999 is reached.
The problem is that the program then continues to run, despite the fact that there are no further function calls, and no return statement. I am wondering whether JavaScript is assuming a return by default (so continuing to run from the next statement in the function that called it), and if I need to add a command to override that.
Rob.
Edited to add:
Quote:
I am wondering whether JavaScript is assuming a return by default...
I've just confirmed that this is what is happenning - I added an alert immediately after the call to outputResult, which displayed as I anticipated. So: I now know why, but not how to stop it.
I've realised that I was thinking of calling a function as being equivalent to using GoTo in BASIC. All the other functions were calling others, or returning values, so I was thinking that as this one didn't, the program would just stop .
Well, I've found a solution, though I don't feel it's particularly elegant. I wrapped
Code:
if (flag != 0)
{
...
}
around the code in the (recursive) generateNumber() function. This does stop further numbers being processed after the limit is reached, but I'd still be interested to know whether there's a JavaScript equivalent of end() or die().
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Such a feature would be very odd, as ending the runtime would mean ending all javascript handlers in the browser, including those not belonging to the caller.
Just like System.exit() is forbidden is Java when the JVM is running in a browser, exitting the Javascript runtime is unwanted, in my opinion.
Calling a function is certainly not like a basic goto, as you figured out, but most like a gosub.
Such a feature would be very odd, as ending the runtime would mean ending all javascript handlers in the browser, including those not belonging to the caller.
But wouldn't it be possible for a function to be added to stop the running of a particular group of functions, possibly using a script tag id?
Anyway, thanks for the answer. This was my first attempt to write a program using several recursive functions (to examine Kaprekar's Operation). Good learning experience. Among other things, I learnt that JavaScript is not really suited to a task like this (it crashed out after processing ~200 numbers each time, due to "Too many recursions"). I may rewrite it in C++, but it's a shame as JavaScript/HTML make it very easy to format the results nicely.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.