2

I was wondering if there is any shorter/ easier way to write a repeating code. If the name entered into the prompt box doesn't have anything then it would send an error message and reput it.

Just so I dont have to explain alot, heres the code:

function error() {
    alert('You must enter a name.');
}

var Name = prompt('What is your name?', 'Name');

function repeat() {
    var Name = prompt('What is your name?', 'Name');
    if(Name === '') {
        error();
        repeat();
    }
}

if(Name === '') {
    error();
    repeat();
}
royhowie
  • 11,075
  • 14
  • 50
  • 67

3 Answers3

8

Like this:

var Name;

while(!(Name=prompt('What is your name?', 'Name'))) {
  alert('You must enter a name.');
}

How it works

The while loop repeats until a condition is met. In this case, the condition is:

!(Name=prompt('What is your name?', 'Name'))

This part of the expression assigns the prompt value to Name (as you're already aware):

Name=prompt('What is your name?', 'Name')

In JavaScript, an assignment to a variable also returns the value. (That's why we can chain assignments such as a = b = c = 16.)

So if you enter "Johnathan" as the name, this expression becomes "Johnathan":

(Name=prompt('What is your name?', 'Name'))

If you enter nothing as the name, the expression becomes a null string.

The logical NOT operator (!) before an expression returns the boolean opposite of the "truthiness" of the expression. A string value is truthy, but a null string is falsy.

By applying the NOT operator to the expression:

!(Name=prompt('What is your name?', 'Name'))

… the loop will continue until the Name variable has a value.

Final thought: By convention, variables should begin with a lowercase letter. I haven't done that here, because name is a property of window, and changing the window's name could lead to problems. Ideally, your prompt would be within a function, so that you wouldn't have any global variables. If that were the case, you could use the variable name as others have suggested.

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
  • @MaxZoom: I don't think you and I were looking at the same answer. And basically: ***No*** code is self-explanatory *for a beginner*. (The current answer is excellent.) – T.J. Crowder Jun 02 '15 at 22:15
2

While I like doing it all in a while loop, and would have done it that way myself. It's also worth understanding the difference between it and a do..while loop, which may help a beginner read things sequentially and understand each step.

var name;
do {
  name = prompt('What is your name?', 'Name');
  if (name == '') {
    alert('You must enter a name.');
  }
} while (name == ''); // jump back to first line of *do* body if true.
dmarlow
  • 417
  • 7
  • 13
  • I agree. Even if more verbose, this approach is much easier for a beginner to read. – royhowie Jun 02 '15 at 22:10
  • Thanks, that helps me out alot! But whats the difference between `==` and `===`? –  Jun 04 '15 at 22:14
  • Take a look here: http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – dmarlow Jun 07 '15 at 22:19
0
function repeat() {
   var Name = prompt('What is your name?', 'Name');
   if(!Name) {
      repeat();
   } else {
       return;
   }
}

Just call itself inside the function, exit only upon condition are met.

aahhaa
  • 2,240
  • 3
  • 19
  • 30
  • 2
    The `else` part is unnecessary. Also `function repeat()` is a syntax error; just use `repeat()` – royhowie Jun 02 '15 at 21:27
  • 1
    And the `function repeat();` part is a syntax error. – T.J. Crowder Jun 02 '15 at 21:28
  • 1
    Not a good idea, to recursively call the repeat function in this instance. Beter to use a while loop. – phuzi Jun 02 '15 at 21:29
  • @phuzi there's really no difference – royhowie Jun 02 '15 at 21:30
  • @royhowie functionally, no, there is no difference but each time the function gets called it pushes it's context on to the stack. if the user is persistent enough to keep entering an empty value, eventually the stack size limit will be reached and an appropriate exception thrown. There's no reason to use recursion when a while loop is much more appropriate. – phuzi Jun 02 '15 at 21:36
  • 1
    @royhowie, I think there's a pretty huge difference if the user's ENTER key gets stuck. Not a good idea to assume your JavaScript engine implements a tail-recursion optimization. – 0xbe5077ed Jun 02 '15 at 21:36
  • Really no need to use recursion here and it appears that the point of the question is how to do it better than a recursive solution since that is what the OP already shows. – jfriend00 Jun 02 '15 at 21:44
  • @MaxZoom don't edit answers, if you're just going to introduce a syntax error – royhowie Jun 02 '15 at 22:20