Variable zeroed in javascript

Asked

Viewed 51 times

0

//Nosso time
var nomeTime = prompt("Qual o nome do seu time ?");
var vitorias = parseInt(prompt("Quantas vitórias seu time teve ?"));
var empates = parseInt(prompt("Quantos empates seu time teve ?"));
var derrotas = parseInt(prompt("Quantas derrotas seu time teve ?"));
var totalJogos = 0;
var totalPontos = 0;
//Oponente
var nomeTimeO = prompt("Qual o nome do time Oponente?");
var vitoriasO = parseInt(prompt("Quantas vitórias ele teve ?"));
var empatesO = parseInt(prompt("Quantos empates ele teve ?"));
var derrotasO = parseInt(prompt("Quantas derrotas ele teve ?"));
var totalJogosO = 0;
var totalPontosO = 0;
//Funções
    //Calcula jogos
function calculaJogos(vitorias, empates, derrotas, vitoriasO, empatesO, derrotasO){
    totalJogos = vitorias + empates + derrotas;
    totalJogosO = vitoriasO + empatesO + derrotasO;
    return totalJogos;
    return totalJogosO;
};
    //Calcula pontos
function calculaPontos(vitorias, derrotas, empates, vitoriasO, derrotasO, empatesO){
    var pVitorias = vitorias * 3;
    var pVitoriasO = vitoriasO * 3;
    totalPontos = pVitorias + empates - derrotas;
    totalPontosO = pVitoriasO + empatesO - derrotasO;
    return totalPontos;
    return totalPontosO;

};
//Call functions
calculaJogos(vitorias, empates, derrotas, vitoriasO, empatesO, derrotasO);
calculaPontos(vitorias, empates, vitoriasO, empatesO);
//Resultados
document.write("O time <b>" + nomeTimeO + "</b> jogou um total de " + totalJogosO + " jogos, tendo feito " + totalPontosO + " pontos. <br>");
document.write("Vitórias: " + vitoriasO + "<br>Derrotas: " + derrotasO + "<br>Empates: " + empatesO + "<br>");
//Comparações
if(totalPontos < totalPontosO){
    document.write("Seu time está indo <b>pior</b> que " + nomeTimeO);
}else if(totalPontos > totalPontosO){
    document.write("Seu time está indo <b>melhor</b> que " + nomeTimeO);
}else if(totalPontos == totalPontosO){
    document.write("Seu time está <b>empatado</b> com " + nomeTimeO);
};

Given the above code, I wanted to understand why:

  • if the latter is just a single Else, even if it is above or below the number, the same draw warning appears
  • the value of points of the opponent "totalPontosO", is and is reset, however, my points work perfectly.
  • Your second question is impossible to answer if you do not state what data is being used. Another thing, a defeat really counts for a point less?

  • 1

    You have 2 Return in the same function, it does not exist. When you give Return the function ends, nothing below runs, you need to return an object or array with the two results, or call the function twice with different parameters.

2 answers

1

Some things about your code:

1) If you use only Else at the end, it will only be executed if there is an equality, since you are testing by smaller and larger, the only different result of this would be equality, then an Else at the end is appropriate.

2) You have 2 Return in the same function, it does not exist. When you give Return the function ends, nothing below runs, you need to return an object or array with the two results, or call the function twice with different parameters.

 function calculaPontos(vitorias, derrotas, empates, vitoriasO, derrotasO, empatesO){
    var pVitorias = vitorias * 3;
    var pVitoriasO = vitoriasO * 3;

    totalPontos = pVitorias + empates - derrotas;
    totalPontosO = pVitoriasO + empatesO - derrotasO;

    var resultado = [totalPontos, totalPontosO];
    return resultado;
};

An idea of how to solve your problem using an array with the function result.

  • I used Else, but as explained, even though it was bigger or smaller, it kept appearing

1


Primarily: A function only goes through one and only one Return (but the Returns in your function are useless because you’ve already changed the global scope variables before Return)

Now, for your Nan problem: You have set the Function calculationPontos receiving 6 parameters, but you are only passing 4 parameters to it:

calculaPontos(vitorias, empates, vitoriasO, empatesO);

Maybe you have doubts about how a Function works, so I’ll explain: Function will receive the parameters according to the position in which they were set in Function and not according to the name.

var some = function (a, b) { return a + b }
var x = 1;
var y = 2;
some(x, y);

In this case the variable a of Function is receiving the value of the variable x global and the variable b of Function is receiving the variable y global. When Voce does not pass all the parameters of the funtion, the variables that do not receive value stay with the Undefined value.

In your case, as you have not passed all the values you are trying to do a mathematical operation of a number with two undefineds, any number with mathematical operation with one Undefined results in one Nan (Not a Number).

Browser other questions tagged

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