Keep variable value between layers of a function

Asked

Viewed 146 times

1

I wanted to make a site where when I click on the image it doubles in size and width, but can do this only once, and if I do more, a warning appears preventing it. Similarly, if it is the default size, you cannot decrease it, and a warning will appear, but if you increase it and try to decrease it, this will be possible.

I stopped at the code around here:

function passar(img){
        var d = 0;
        if (d == 0){
        var x = confirm("tem certeza que deseja aumentar a imagem?");
        if(x == true && d ==0){
        d=d+1;
        img.height=img.height*2;
        img.width=img.width*2;
        }
        else if (x== true && d==1)
        window.alert("Não é possivel aumentar mais");
        else{
        var y = confirm("tem certeza que deseja diminuir a imagem?");
        if ( y==true && d==1){
        d=d-1;
        img.height=img.height/2;
        img.width=img.width/2;
        }
        else if(y == true && d==0)
        window.alert("Não é possivel");
        }}}

My biggest problem is being that when one if-else locks the key, the d does not possess the new value I gave it d=d+1 or d=d-1, I’ve tried other ways but I can’t find a solution.

  • Try to use d += 1 and d -= 1

  • did not take, continued the same way, when it closes the key of the IF-ELSE the d(counter) loses the value, being limited to the own if and Else.

1 answer

2


Organizing the code always helps, it’s very easy to fool where each if close, it’s a nightmare to give maintenance in code so I lost several minutes to understand what you’re really doing and I have my doubts if I got it right. The name of the variables should be more significant. I did not check if the logic is correct.

In this case it is necessary to make the variable be considered from the instance of the function to its value maintained between executions. Also it should only boot if it has not been initialized, so we use a relational operator

function passar(img) {
    passar.contador = passar.contador || 0;
    if (passar.contador == 0) {
        var confirma = confirm("Tem certeza que deseja aumentar a imagem?");
        if (confirma && passar.contador == 0) {
            passar.contador++;
            img.height *= 2;
            img.width *= 2;
       } else if (confirma && passar.contador == 1) {
            window.alert("Não é possível aumentar mais");
        }
    } else {
        var confirma = confirm("Tem certeza que deseja diminuir a imagem?");
        if (confirma && passar.contador == 1) {
            passar.contador--;
            img.height /= 2;
            img.width /= 2;
        } else if (confirma && passar.contador == 0) {
            window.alert("Não é possível diminuir mais");
        }
    }
}
var img = {height : 10, width : 10};
passar(img);
passar(img);

I put in the Github for future reference.

I left as an accountant because in the future you may want to allow new scales, in the current form, nor need to be a numerical counter, since it only has two possible states.

Of course there are other solutions.

  • So I used your method (I gave Ctrl Ctrl v hehe) and it didn’t work. I’m sorry for the bad organization, it’s just that for me it’s easier. I took some things like "img.height *= 2;", but it didn’t work, all the javascript stopped picking up. Here’s the organized site: http://pastebin.com/brpgW2zi ( can’t be more than 600 characters, so I sent it in link form ) anyway, thanks for the help ^^

  • By force of habit I put one var where I shouldn’t. Now I put a test

Browser other questions tagged

You are not signed in. Login or sign up in order to post.