1

I've tried several ways to do this but I can't, I need to repeat to unlimited the function nombreintroducido until there's a value that's not '', and this script only works once, it repeats it only once.

var nombrepersonaje = function(name) {
  return nombre = prompt("Como quieres que se llame tu personaje");
  nombreintroducido();
}

nombrepersonaje();

var nombreintroducido = function (introducido){
  if (nombre === '') {
    confirm('Tu nombre de personaje ha de tener mas de 3 caracteres');
    nombrepersonaje();
  } else {
    confirm('Tu nombre de personaje es' + ' ' + nombre)
  }
};

nombreintroducido();

1
  • Shouldn't nombrepersonaje() be in else as well? Commented Dec 24, 2015 at 14:00

2 Answers 2

1

Code after a return statement does not run. I think you want something like

var nombrepersonaje = function() {
  var nombre = prompt("Como quieres que se llame tu personaje");
  nombreintroducido(nombre);
};
var nombreintroducido = function(nombre){
  if (nombre === '') {
    confirm('Tu nombre de personaje ha de tener mas de 3 caracteres');
    nombrepersonaje();
  } else {
    confirm('Tu nombre de personaje es' + ' ' + nombre)
  }
};
nombrepersonaje();

Sign up to request clarification or add additional context in comments.

Comments

1

What you're looking for is the while loop.

var nombreintroducido = function (introducido) {
    while (nombre === '') {
        setTimeout(prompt('Tu nombre de personaje ha de tener mas de 3 caracteres'), 1000);
        nombrepersonaje();
    }
};

2 Comments

It should be prompt and not confirm
I would add a delay in there, else most browsers will start complaining :)

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.