1. Debating Self-Existence
Join Date
Jun 2003
Location
Lost in the depths of insanity
Posts
101

"goto" in JavaScript

I havent used JavaScript much but I was planning on using it to replicate a program I wrote in Liberty BASIC. Is it possible to "goto" a set location for example, if one variable equals another you skip a chunk of code and go on to the next bit?
I have added the Liberty BASIC code I am attempting to replicate below. It calculates the PMCC for the values entered with a maximum of ten sets of data.

[start]
input "How many sets of values do you have? "; sets
let setscount = 0
if sets = 0 then [help]

[xvalues]
input "Enter the first x value. "; xone
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xtwo
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xthree
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xfour
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xfive
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xsix
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xseven
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xeight
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xnine
let setscount = setscount + 1
if setscount = sets then [yvalues]

input "Enter the next x value. "; xten
let setscount = setscount + 1
if setscount = sets then [yvalues]

[yvalues]
let setscount = 0
input "Enter the first y value. "; yone
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; ytwo
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; ythree
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; yfour
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; yfive
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; ysix
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; yseven
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; yeight
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; ynine
let setscount = setscount + 1
if setscount = sets then [calc]

input "Enter the next y value. "; yten
let setscount = setscount + 1
if setscount = sets then [calc]

[calc]
let xbar = (xone + xtwo + xthree + xfour + xfive + xsix + xseven + xeight + xnine + xten)/sets
let ybar = (yone + ytwo + ythree + yfour + yfive + ysix + yseven + yeight + ynine + yten)/sets
let sumxy = (xone*yone)+(xtwo*ytwo)+(xthree*ythree)+(xfour*yfour)+(xfive*yfive)+(xsix*ysix)+(xseven*yseven)+(xei ght*yeight)+(xnine*ynine)+(xten*yten)
let sumx = xone + xtwo + xthree + xfour + xfive + xsix + xseven + xeight + xnine + xten
let sumy = yone + ytwo + ythree + yfour + yfive + ysix + yseven + yeight + ynine + yten
let sumxsumy = sumx*sumy
let sumxsq = (xone^2)+(xtwo^2)+(xthree^2)+(xfour^2)+(xfive^2)+(xsix^2)+(xseven^2)+(xeight^2)+(xnine^2)+(xten^2)
let sumysq = (yone^2)+(ytwo^2)+(ythree^2)+(yfour^2)+(yfive^2)+(ysix^2)+(yseven^2)+(yeight^2)+(ynine^2)+(yten^2)
let sumxallsq = sumx^2
let sumyallsq = sumy^2
let bottomxbrac = sumxallsq/sets
let bottomybrac = sumyallsq/sets
let bottomx = sumxsq-bottomxbrac
let bottomy = sumysq-bottomybrac

let top = sumxy-(sumxsumy/sets)
let bottom = bottomx*bottomy
let rootbottom = SQR(bottom)
let pmcc = top/rootbottom

print
print "The PMCC of the numbers you entered is: "; pmcc

[rerun]
input "Run again? (Enter yes or no) ";rerun\$
if rerun\$ = "yes" then [start]
end

[help]
print "This program calculates the PMCC of the numbers entered."
print "First enter the number of x and y values you have and then"
print "enter the x and y values themselves. You must enter more than"
print "one set of data."
print
print

Sorry its long but I have only just started programming so I dont know any other way. (Could you do it with arrays maybe?)
If you know any way I can either shorten the code or neaten it up please tell me.

Thanks

2. goto is not a part of JavaScript. It is a command that has been defunct for quite some time now. More structured statements such as if, else, do, while, for and subroutines have taken its place to make code much more readable and manageable.

3. Debating Self-Existence
Join Date
Jun 2003
Location
Lost in the depths of insanity
Posts
101
So how would I perform an equivalent function in JavaScript that works along these lines?

if somevariable = anothervariable then (jump to some part of the script) else carry on as normal.

(This isn't proper script this is just what it should do roughly.)

Thanks

4. Registered User
Join Date
Jan 2003
Location
Atlanta, GA
Posts
571
There is nothing goto does that can't be done equally easily using other conditional statements (if, which, break, continue etc).
EXAMPLE:
---------
if (a > b) {
goto *}
[Set of commands 1]
*
[Set of commands 2]

EQUIVALENT METHOD
---------------------
if (a <= b) {
[Set of commands 1] }
[Set of commands 2]

WORST CASE
-------------
if (a > b) {
[Set of commands 2] }
else {
[set of commands 1]
[set of commands 2]}

5. Debating Self-Existence
Join Date
Jun 2003
Location
Lost in the depths of insanity
Posts
101

So does that mean I would have to repeat the whole code over and over? Is there anyway where I could get people to download and install a plugin enabling there pc to run the Liberty Basic script so I could use the Liberty Basic script I have already made in my website. Another alternative would be to let them download the Lib Basic script as an exe file but at the moment it is just a .bas so I dont know how I would convert it into an exe. Any ideas anyone
??

Thanks

6. So does that mean I would have to repeat the whole code over and over?
No. There's no reason you should ever have to type a section of code more than once. In fact, making sure it is in only one location makes the program easier to maintain.

7. We're talking here about "structured programming". In the old days we would learn a language called Pascal to teach us good structured programming practices. That was twenty years ago and I remember taunting the people who were still using "goto" statements. See http://acweb.colum.edu/users/rcourin.../struprog.html for a brief introduction. You'll have to change your thinking about how a program works, but just a bit. When you get to "object oriented programming" (OOP) you'll have to change your thinking a great deal. Oh, and JavaScript is an object oriented language but you don't have to treat it that way.

8. Debating Self-Existence
Join Date
Jun 2003
Location
Lost in the depths of insanity
Posts
101
So what would I do? Set each different bit as a functiion and call them when needed?
I only started learning about a month ago using online tutorials and teaching myself so I dont know anything really.

Thanks

9. Debating Self-Existence
Join Date
Jun 2003
Location
Lost in the depths of insanity
Posts
101
OK, I have looked at the webpage and will make a concerted effort to do away with "goto" statements but until I learn otherwise is there anyway that I can write the above Liberty Basic code in a format that I can use on a website to produce the same effect by any means? The script needs to be executed when a user acceses the page or maybe using a form so the fields are filled in and a button is pushed to make the code run and the pmcc is popped out into a little text box.

Thanks

10. Here's in English what I have in mind as a way of doing this. Initialize a variable to zero for the number of sets. Use a while loop to have the user enter a number within the range of 1 to 10. They can not exit the while loop until they do so. If they enter something outside of the intended range, remind them of the range, and have them reenter the number of sets.

Once they do that, you create arrays for each x and y set of numbers. The size of the arrays will be equal to the number of sets the user entered. Use a for loop that lets the user enter the x value, and then the y value for an instance of the array. Loop through the array until you are past the last position in the array.

For the calc part you can use loops if you to add up each instance of the array and then divide the total by the length of the array. You can do this for both x and y arrays within the same loop. Next you just multiply the first instances of x and y together and go through calculating all the other stuff. Again, use a for loop that stops once you've factored in all of the instances of the arrays.

From there on it's pretty straight forward because anything after this point should be easy to figure out since the answer is most likely going to be something you already did, or very straight forward. The rerun part just means you put the whole thing into a loop and exit out of it when the user is done.

I don't think you'll really need to make functions in this one, but you are going to have a ton of loops and most of them being for loops. Your code will be much more flexible than what it is now though since the number of times you use most of the loops will be based on the number of sets that person is entering.

11. Registered User
Join Date
Jan 2003
Location
Atlanta, GA
Posts
571
OK, this is not Javascript...
Code:
```rerun = 'y'
while (rerun == 'y') {
sets = input ('Total Number');
if (sets == 0) {
[help text];
continue; /* ends the loop */ }
for i=1:i++:sets {
x[i] = input('x');
y[i] = input('y');
}
[calc...]
rerun = input('Rerun (y/n)?');
}```

12. Debating Self-Existence
Join Date
Jun 2003
Location
Lost in the depths of insanity
Posts
101
Thanks spufi,
Does that mean that its possible to have more than ten sets of data if the array size was increased? The only reason I restricted it to ten was the original code was big, clumsy and chunky and I didn't want to have to type similar code repeatedly.

Thanks

13. Since the size of the arrays will be based on how many sets the person asks for, yes, it can have more than 10 if you want it to. Your code to do it will look something like this.

var numSets = prompt("Enter number of sets", 0);

xArray[numSets] = new Array();
yArray[numSets] = new Array();
Last edited by spufi; 07-03-2003 at 10:17 AM.

14. Debating Self-Existence
Join Date
Jun 2003
Location
Lost in the depths of insanity
Posts
101
Thanks,
How would I get the x and y values the user inputs to be stored in the array and how would I call values from an array?

Thanks again#

15. for (i = 0; i = numSets; i++)
{
xArray[i] = prompt("Enter x value for set number " + (i + 1), 0);
yArray[i] = prompt("Enter y value for set number " + (i + 1), 0);
}

That's the basic idea.