Contador Javascript

Asked

Viewed 268 times

0

Hi guys I’m not knowing how I put an if’s dentros counter every time I have an approved or disapproved for later I show in a table the amount

<script>
    function calcula_media(i)
    {
        var n1 = document.getElementById("n1_aluno_" + i).value;
        var n2 = document.getElementById("n2_aluno_" + i).value;
        var media = document.getElementById("media_aluno_" + i);
        var resultado = document.getElementById("resultado_aluno_" + i);
        var calculo = (parseFloat(n1) + parseFloat(n2)) / 2;
        var count = 0;

        if(calculo >= 70)
        {
            media.innerHTML = "<div class='text-success'>" + calculo + "</div>";
            resultado.innerHTML = "Aprovado";
            
        }
        else if(calculo >=30)
        {
            media.innerHTML = "<div class='text-rec'>" + calculo + "</div>";
            resultado.innerHTML = "Recuperaçao";
        }
       
        else
        {
            media.innerHTML = "<div class='text-danger'>" + calculo + "</div>";
            resultado.innerHTML = "Reprovado";
        }
    }
    </script>

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

I put the counters. But a function that is said to calculate average should not count approved, at least the name should change. The code ends up doing more than it should and can give errors in some situations. I will not try to solve all issues.

Note that I initialized a variable for each type of count and incremented the counter whenever it enters each of the ifs according to the criterion. Still need to present these counters at the end somehow.

function calcula_media(i) {
    var n1 = document.getElementById("n1_aluno_" + i).value;
    var n2 = document.getElementById("n2_aluno_" + i).value;
    var media = document.getElementById("media_aluno_" + i);
    var resultado = document.getElementById("resultado_aluno_" + i);
    var calculo = (parseFloat(n1) + parseFloat(n2)) / 2;
    var contaAprovados = 0;
    var contaRecuperacao = 0;
    var contaReprovados = 0;
    if (calculo >= 70) {
        media.innerHTML = "<div class='text-success'>" + calculo + "</div>";
        resultado.innerHTML = "Aprovado";
        contaAprovados++;
    } else if (calculo >=30) {
        media.innerHTML = "<div class='text-rec'>" + calculo + "</div>";
        resultado.innerHTML = "Recuperaçao";
        contaRecuperacao++;
    } else {
        media.innerHTML = "<div class='text-danger'>" + calculo + "</div>";
        resultado.innerHTML = "Reprovado";
        contaReprovados++;
    }
}

I put in the Github for future reference.

  • The problem is that these variables, being within the function, will never be greater than 1. :/

  • 1

    The problem does not speak of this and put away is not the solution, it may work, but it will be a beautiful gambit. That’s what I said, a function that takes care of calculating the average should not count anything. All design of this is wrong.

  • 2

    I don’t see anything wrong. She is taking different input values, elements with different id’s and calculating the average between two values, inserting the result in stylized div form for each situation, and wants to count the number of each situation globally... by the way, only two of the 3 situations: approved and failed.

  • 1

    There are no inputs in the question. Global variables are quite wrong, everyone knows this. It has better solution. I can’t imagine why the third situation is not wanted, seems just another mistake, the whole question seems pretty bad and the decision taken on it will certainly be bad.

  • 1

    This is your vision seems to me very distorted. It has input yes, if you observe the code. Variables don’t need to be global exactly, and your code doesn’t work. So you shouldn’t answer if it doesn’t work. -1

  • The curious thing is that your answer gets 2 positive votes when it doesn’t work.

  • I’ll even remove my answer because the same AP has already asked 2 questions with the same subject and it all seemed confusing to me. She asked two equal questions with different accounts, but this the moderators do not seem to see. One is that one and the other she did with a similar username, "Stella" sei la oq.

  • 1

    Where is there input? You’re having hallucinations, anyone can see that you don’t have.

  • 2

    Ah, there is another question. I answered what I had here. In fact this question is bad. Moderators are not omnipresent, if you see something and do nothing the cullpa is yours of something not to be done. You were doing well not complaining anymore, because your complaints are always based on things that are only in your head. We think the person learned, but that’s not it.

Show 4 more comments

Browser other questions tagged

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