1

I'm wondering how can I export a variable from inside a function to be used in another functions, I've used this code:

But in the latest function I'd like to add a variable called nombre from another function, but I'm not able, I thought about adding several values to a function, but at last, I only can imagine a function with endless parameters, if that's possible.

Thanks in advance! Cristobal.

<script>
//Empieza el script justo aquí
//Aquí definimos la función de cómo queremos llamar a nuestro personaje
//La función nombreintroducido recoge el valor de la variable nombre y la usa más adelante
  var nombrepersonaje = function() {
    var nombre = prompt("Como quieres que se llame tu personaje");
	nombreintroducido(nombre);
  }

//Aquí definimos que si el nombre tiene menos de tres carácteres, se repite la función nombrepersonaje
//Si se pulsa cancelar, se terminará el juego
//Si introduces algún nombre de personaje que sea válido, se abrirá un mensaje emergente que mostrará el nombre introducido
var nombreintroducido = function (nombre){
if (nombre === '') {
  confirm('Tu nombre de personaje ha de tener mas de 3 caracteres');
  nombrepersonaje();
} else if (nombre === null){
confirm('No has introducido ningun nombre de personaje, el juego terminara ahora')
}
else{
  confirm('Tu nombre de personaje es' + ' ' + nombre)

  }
};

var eligeclase = function(){
var clase = prompt("Que clase quieres elegir: Guerrero o Mago")
claseescogida(clase);
}

var claseescogida = function (clase){
if (clase === 'Guerrero'){
confirm('Has elegido la clase Guerrero: 10 Fuerza y 5 Inteligencia');
confirmaclase(clase);
}
else if (clase === 'Mago') {
confirm ('Has escogido la clase mago: 10 Inteligencia y 5 Fuerza');
confirmaclase(clase);
}
else {
confirm ('Tienes que escribir exactamente Guerrero o Mago');
eligeclase();
}};

var confirmaclase = function(clase) {
confirm('Tu clase es finalmente ' + clase + ' ... y tu nombre es');
}


//Se podría decir que el minijuego empezaría aquí, ya que lo anterior son funciones que definimos
nombrepersonaje();
eligeclase();



//Termina el script justo aquí
 </script>

2
  • Just use global variables so that they can be referenced and altered anywhere Commented Dec 24, 2015 at 19:37
  • And how can I do that? How can I use global variables? I'm just doing a course in another site, I started learning few days ago, don't know why I was voted -1, there's no place for newbies here? :( The variable is defined by a prompt in a function, so if it's written in a function and asked in a function, it can't be global, right? How then? Commented Dec 24, 2015 at 19:40

2 Answers 2

3

You can do it two ways:

  1. Declare var globally so that variable can be used from anywhere(inside or outside of java script) like.. var nombre; declared at top of javascript, and can be initialize and used within a javascript and from other java script as well. You need to just import the source js.
  2. Create a function which return the var itself. like.

    var myFunction = function(){return nombre;}

    When you need the nombre variable, just call the function as, var newNombre = myFunction();

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

1 Comment

If the var itself is being returned, when I call this variable after, do I have to call the variable or the function? I'm just trying this but I get variable as undefined.
0

You can define a variable globally like this (For example in variables.js):

var myNombre;

Now whenever you want to prompt the user, you can assign the input to myNombre:

function initMyNombre () {
    // assign from input
    myNombre = prompt("Enter a nombre");
}

You variable now holds the input and keeps it until either you reassign it or close the browser tab.

Then, maybe in your index.html, you can use myNombre:

function anyFunction () {
    // x now has the value of myNombre
    var x = myNombre;
}

Note that you need to include variables.js in your html document with <script src="directory/variables.js"></script>. Also have a look at This W3schools Tutorial. Eventhough W3schools isn't the best practice for seasoned programmers, it can give you a good introduction.

Comments

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.