Problem when breaking in if

Asked

Viewed 452 times

3

If I put one break in the first loop if, simply does not work or execute anything.. It seems that the break is read first of all, even without entering the condition if. What’s the matter?

function verifynasc(field) {
    var nascimento = field.value;
    alert(nascimento);
    if (nascimento.length > 10) {
        alert("entrou no if");
    }
    nascimento = nascimento.replace('/', '');
    var dia = '';
    var mes = '';
    var ano = '';
    for (var i = 0; i < nascimento.length; i++) {
        if (i < 2) {
            var dia = dia + nascimento.charAt(i);
            continue
        } else if (i < 4) {
            var mes = mes + nascimento.charAt(i);
            continue
        } else {
            var ano = ano + nascimento.charAt(i);
        }
    }
    var data = ano + "-" + mes + "-" + dia;
    document.getElementById('date').value = data;
}
  • What is the problem you have? can you explain it better? (suggestions however: use nascimento = nascimento.replace('/', ''); instead of this replaceAll, and join var inside the go to stay for (var i = 0; etc...)

  • Problem is, I wanted that when you enter for IF condition it de-break up! only that if I break to do such action, even not entering the IF condition my code goes to bag. all my document that is contained the javascript codes do not work.. replaceAll I use to replace all and not only one, was a function I created for this.

  • @Alexandrec.Caus If your loop starts on 0 and the first if forehead for i < 2 then he at all times will enter the if and he at all times will come out of the loop at this point! If you’re saying your code whole stops working when placing the break, can only be a syntax error (you wouldn’t be by chance putting break and continue in the same if, would be? And by the way, always use ;, does not matter if the language requires it or not). Open in Chrome for example, and see on the console if it is displaying syntax error.

  • if it is another, this one you are pointing is the second, before the loop itself is.

  • @Alexandrec.Caus, what values can have the variable nascimento?

2 answers

3

Your code could be improved, restructure these ifs, so you don’t need the Return, that you thought was the continue.

The problem is that break and continue are for LOOPS, not for IF

Instead of going on, you’d use Return in the if.

The command break "jumps out" of a loop (loop).

The command continue "jumps only" one loop interaction (loop).

function verifynasc(field) {
    var nascimento = field.value;
    alert(nascimento);
    if (nascimento.length > 10) {
        alert("entrou no if");
    }
    nascimento = nascimento.replace('/', '');
    var dia = '';
    var mes = '';
    var ano = '';
    for (var i = 0; i < nascimento.length; i++) {
        if (i < 2) {
            dia = dia + nascimento.charAt(i);
        } else if (i > 3 && i < 4) {
            mes = mes + nascimento.charAt(i);
        } else {
            ano = ano + nascimento.charAt(i);
        }
    }
    var data = ano + "-" + mes + "-" + dia;
    document.getElementById('date').value = data;
}

1

Assuming I read your code correctly, you’re trying to garland that:

  • A date entered with / is then sent with -;
  • A date in the format "DD-MM-YYYY" is then sent in "YYYY-MM-DD".

Something simpler could be:

Example in Jsfiddle

HTML

<input value="" onblur="formatDate(this)" onkeyup="this.value=this.value.replace(/\-/g,'/')">

JS

function formatDate (field) {
    var date = new Date(field.value);
    var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
    if (month < 10) month = "0" + month;
    if (day < 10) day = "0" + day;

    field.value = year + "-" + month + "-" + day;   
}

The idea is to have the event blur to call the function that will format the date, while we have the event keyup to change the - for / so that the formatting function works properly.

In this way avoid cycles and checks via IF~ELSE.

Browser other questions tagged

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