1

I'm learning some basic javascript, and would like to learn how to return to the beginning of a method when certain conditions are met.

In this scenario, the user must enter a character in the prompt in order to proceed to the statment "The character you typed was" string. I would like to implement a loop to send the program back to the start of the method if nothing is entered. I have the following, so far:

<script class="promptwindow">            
        var x
        x = prompt("Please type a character in the box and click OK", "")

        if (x = null)
            {****}

        document.write("The character you typed was ", x)    
</script>

I'm unsure what to use in the **** bracket, I need something similar to goto.

Edit: Yep, should have been ==. I'll leave the mistake there so that the comments make sense.

2
  • 3
    class="promptwindow" in script tag? Commented Aug 27, 2014 at 12:16
  • 3
    no -> if (x = null) yes -> if (x == null) Commented Aug 27, 2014 at 12:17

2 Answers 2

3

To go back to the beginning of your function, just call it again:

<script type="text/javascript">
   ( function myFunction() {
        var x;
        x = prompt("Please type a character in the box and click OK", "")
        if (x === null)
            myFunction();
        document.write("The character you typed was ", x);
    })();
</script>

Be carreful to the document.write()'s position, when x will not be null anymore, all stacked document.write("The character you typed was ", x); will be executed

You can also use a loop...

<script type="text/javascript">
    function myFunction() {
        var x = null;
        while(x === null || x === ""){
            x = prompt("Please type a character in the box and click OK", "")
        }
        document.write("The character you typed was ", x);
    }
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Brilliant! I was hoping for something like this, +1.
Erm... actually, this now does nothing at all. It's now just a blank page.
@Wolfish Of course you have to call the function once to start it
Didn't seem to work. I'm using OnLoad in the <body> tag, but the loop doesn't seem to work. Edit: made the self-invocation work, but the loop is still broken.
Actually prompts returns an empty string instead of null, the loop's condition should be updated
1
<script type = "text/JavaScript">
    function UserInput(_callback_)
    {
        var value = "";
        do
        {
            value = prompt("Please type a sentence in the box and click OK", "");
        }
        while(value === "");

        _callback_(value);
    }

    UserInput(function(text){
        document.write("The sentence you typed was { " + text + " }");
    });
</script>

http://jsfiddle.net/dqda8Lgk/

I hope it will help you :)

1 Comment

This is a little more advanced, I'll be looking at callbacks later on today. I appreciate the snippet though, +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.