Control in loops and if?

2

I'm putting together a kind of Narrated Role using Javascript.

I use a <input id="dialogo" type="text"> to place our answers around the situation that is happening in the "Adventure".

I take the data of <input> when the user presses the "Enter".

var dialogo = document.querySelector('#dialogo');
dialogo.addEventListener('keydown', ComprueboDatos)


function ComprueboDatos(){

    var tecla = event.key;

    if(tecla == 'Enter'){ //Trate de poner 13 pero no me lo toma. (ASCII ENTER)
        if(dialogo.value == "aventura"){
           dialogo.value = ''
           IniciaAventura()
        }
        else{
            consola.innerHTML = "No entendi lo que me dijiste."
            dialogo.value = ''
        }
    }
}

Now, when I want to confirm if you really want to start it, the idea is to rewrite the <input> "yes" and start it.

function IniciaAventura() {

    if(dialogo.value == 'si'){
        consola.innerHTML = "Empezemos a explorar";

        setTimeout(AventuraNro1, 2000);
    }
     if (dialogo.value == 'no') {

        CancelaAventura();
     } 
     else{
        consola.innerHTML = "Vuelve cuando estes preparado."
     }
}

But obviously I have 2 cases:

  • One is that you set dialogo = '' to delete the content that is written.

  • And second, it's all linear. For more than write "adventure", I started the "adventure", passes the dialogo emptied without leaving me ami option to decide and ends in an else.

My question

How can I do to have more control within the loops and if in similar cases?

This example that I showed is not so much the problem, but imagine when you have to start describing the user if he decides to go east or west or other types of written actions.

I would need to constantly use <input> and have it respond to me in the place I requested it.

Extra:

Investigate using Break and Continue, but do not find a solution with those commands.

I hope it has been clear, I hope you can give me a hand to guide me better.

It's my first big project with HTML, CSS / Sass / JS / Gulp / PHP / MySQL so I'm sure when I fall back into the well I'll ask again.

Thank you very much!

    
asked by Alfacoy 30.11.2017 в 22:26
source

2 answers

2

Of course you can use your input for both things:

The only thing I did was to remove the event ComprueboDatos() from input so that when you press enter I did not run it again and add a new one, which would be used to confirm.

NOTE: If you want to capture the key by its code and not by its name then you should use event.keyCode

var consola = document.querySelector('#consola');
var dialogo = document.querySelector('#dialogo');
dialogo.addEventListener('keydown', ComprueboDatos);


function ComprueboDatos(){
    var tecla = event.keyCode;

    if(tecla == 13){
        if(dialogo.value == "aventura"){
           dialogo.value = ''

           consola.innerHTML = "¿Estás preparado?"

           dialogo.removeEventListener('keydown', ComprueboDatos);
           dialogo.addEventListener('keydown', IniciaAventura);
        }
        else{
            consola.innerHTML = "No entendi lo que me dijiste."
            dialogo.value = ''
        }
    }
}

function IniciaAventura() {
    var tecla = event.keyCode;

    if(tecla == 13){
        if(dialogo.value == 'si'){
            consola.innerHTML = "Empezemos a explorar";

            setTimeout(AventuraNro1, 2000);
        }else if (dialogo.value == 'no') {
            consola.innerHTML = "Vuelve cuando estes preparado."

            CancelaAventura();
        }else{
            consola.innerHTML = "No entendi lo que me dijiste. <br> ¿Estás preparado?"
            dialogo.value = ''
        }
    }
}
<input type="" name="" id="dialogo">
<div id="consola"></div>
    
answered by 30.11.2017 / 22:40
source
1

I think what you're looking for is to use the confirm method. In this way, you can show the user a question and he will be able to answer "Accept" or "Cancel" and, depending on the answer he selects, execute one code or another.

Example:

if(confirm('¿Estás seguro de que quieres continuar por ese camino?')) {
    console.log("Ha dicho que si");
}
else {
    console.log("Ha dicho que no");
}
    
answered by 30.11.2017 в 22:32