SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript beginner - got stuck due to the lack of GOTO

    I make now the first steps in JavaScript. To practice, I took a game I coded in VBA and try to "translate" it in JavaScript.

    But I got stuck into one issue: JavaScript is missing GOTO. All of a sudden what was so simple in VBA becomes a big obstacle in JavaScript.

    The scenario is simple. I have to add two random integers (both between 0 and 10, let's say). And to move forward only if the condition x+y<11 is satisfied. If not, a new set (x,y) must be generated and tested for the condition.

    In VBA, using GOTO, makes this few lines of code a piece of cake. But in JavaScript, due to the lack of GOTO, I fiddle around pointlessly to find a solution

    I thank you in advance for any advice, to bring me on the right track again.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,175
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    Hi iib, welcome to the forums

    I haven't seen code with GOTO in it for decades!

    GOTO code is procedural and long scripts can easily become "spaghetti".

    Depending on what the lines are doing, I think you'll need to wrap them in functions and instead of GOTO call the function.

  3. #3
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, in the meantime I found myself the solution to my problem

    I solved it using WHILE.

    I am sure that for you was a trivial issue, but for me was a big challenge

    I will come back with the next issue, if will appear. Thanks again for inspiring me

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    As you correctly concluded, while is a good way to solve this problem.

    Here's one way you might go about it:

    Code:
    // Get two numbers between 0 and 20 and only proceed if total >= 35
    
    function getTwoRandomNumbers(){
      var x = Math.round(Math.random()*20),
          y = Math.round(Math.random()*20);
    
      return x + y;
    }
    
    var total = getTwoRandomNumbers();
    
    while (total < 35){
      console.log("Didn't work! Total is: " + total);
      total = getTwoRandomNumbers();
    }
    
    console.log("It worked. Total is: " + total);

  5. #5
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    GOTO? funny. lol
    Quality codes are optimized and tested...
    Click here for inspiration..

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,820
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    GOTO is almost as dead as ALTER GOTO (saw a lot of those in the early 80's in Cobol left over from before they got the PERFORM statement to work properly).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by felgall View Post
    GOTO is almost as dead as ALTER GOTO (saw a lot of those in the early 80's in Cobol left over from before they got the PERFORM statement to work properly).
    I still use GOTO in batch scripting for sys admin stuff, but certainly not in web development.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •