Compare result of a function

1

I'm a beginner in JS. The script below will calculate the points of two teams, which will be informed through the prompt (yes, it's annoying, but it's for testing).

I created (or tried) a function to always do the accounts, regardless of the number of times.

What I want is that, as there are two teams, store the sum of the points of each team and at the end make the comparison, making it state who is better in the championship.

var meuTime = prompt("Digite o nome do time: ");
var vitoriasMeuTime = prompt("Quantas vitórias seu time tem? ");
var empatesMeuTime  = prompt("Quantos empates seu time tem? ");

var timeAdversario = prompt("Digite o nome do time adversário: ");
var vitoriasAdversario = prompt("Quantas vitórias eles tem? ");
var empatesAdversario  = prompt("Quantos empates eles tem? ");

function calculaPontos (time,vitorias,empates){
var pontos = (vitorias * 3) + parseInt(empates)
document.write(time + " tem " + pontos + " pontos! ");
}

calculaPontos (meuTime, vitoriasMeuTime, vitoriasMeuTime);
calculaPontos (timeAdversario, vitoriasAdversario, empatesAdversario);


 if(pontosMeuTime > pontosAdversario){
	document.write("Estamos melhor que eles!");
} else if (pontosMeuTime == pontosAdversario){
	document.write("Empatados com eles!");
} else {
	document.write("Estamos atrás, vamo logo crl!");
}
    
asked by anonymous 30.03.2016 / 17:47

1 answer

3

You are calculating the number of points, but you are not returning the value. This will give you the value to assign to variables pontosMeuTime and pontosAdversario .

var meuTime = prompt("Digite o nome do time: ");
var vitoriasMeuTime = prompt("Quantas vitórias seu time tem? ");
var empatesMeuTime  = prompt("Quantos empates seu time tem? ");

var timeAdversario = prompt("Digite o nome do time adversário: ");
var vitoriasAdversario = prompt("Quantas vitórias eles tem? ");
var empatesAdversario  = prompt("Quantos empates eles tem? ");

function calculaPontos (time,vitorias,empates){
  var pontos = (vitorias * 3) + parseInt(empates)
  document.write(time + " tem " + pontos + " pontos!");
  return pontos;
}

var pontosMeuTime = calculaPontos (meuTime, vitoriasMeuTime, empatesMeuTime);
var pontosAdversario = calculaPontos (timeAdversario, vitoriasAdversario, empatesAdversario);


if(pontosMeuTime > pontosAdversario){
    document.write("Estamos melhor que eles!");
} else if (pontosMeuTime == pontosAdversario){
    document.write("Empatados com eles!");
} else {
    document.write("Estamos atrás, vamo logo crl!");
}

Example in JSFiddle .

    
30.03.2016 / 18:00