Zero variable in javascript

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 code above, I wanted to understand why:

  • If the latter is just a simple else, even if it is above or below the number, the same draw warning appears
  • The points value of the opponent "totalPointsO" is and is zeroed, but my points work perfectly.
asked by anonymous 22.11.2016 / 17:30

2 answers

1

Primarily: A function only passes one and only one return (but the returns in your function are useless because you have already changed the global scope variable before the return)

Now, for your NaN problem: You have defined the function calculaPoints getting 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: The function will receive the parameters according to the position in which they were defined in the 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 the function is receiving the value of the global variable x and the variable b of the function is receiving the variable and global. When you do not pass all the parameters of the funtion, the variables that do not receive value get the value undefined.

In your case, since you have not passed all the values you are trying to do a mathematical operation of a number with two undefineds, any number with a mathematical operation with an undefined results in a NaN (Not a Number).

    
22.11.2016 / 18:11
1

Some things about your code:

1) If you only use else at the end, it will only run if there is equality, since you are testing smaller and larger, the only other result would be equality, so an else in the end is appropriate.

2) You have 2 return in the same function, this does not exist. When you give return the function ends, nothing below is executed, 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 result of the function.

    
22.11.2016 / 17:57