Comparing a function result

Asked

Viewed 99 times

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 is boring, but it is for test).

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

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 inform 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!");
}

  • What do you mean "store the sum of the points of each team"?

  • When the function is first executed, store a variable, e.g.: pontosMeuTime = points - that would be the result of the function -. In the second execution the same thing should happen, and I would create pointsAdversario = points - result of the second execution -.

  • You need to create an array that adds up the results of each moment (an array for each player) and then add everything up when needed. How you call the function that should give the final result?

  • Then.. in the case the example was with two teams. Now thinking of arrays, I imagine that the ideal would be to return the result of the function, as was done below, to only end up comparing between them. In general, the idea of this example was to create a function, fixing the idea of compressing the code, avoiding redundancies, and also the if/Else conditions.. Anyway, thank you very much!

1 answer

3


You are calculating the number of points, but you are not returning the value. With this you will have the value to assign to the 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.

  • It’s true.. I didn’t think to return the result of the calculation. After returning, I only created a variable that ran the function, and compare the value of each one? I believe I understood! Perfect! Ah, and thank you! ;)

  • review its function because I tested and the inserted values that I used generated wrong results, team 1: helm, victories(10), draws(0) // team 2: timinho, victories(0), draws(1)

  • @Wilsonrosagomes, what’s the problem? I used these values in https://jsfiddle.net/rmrdmhbd/2/, and saw no problem in the result.

  • tested now and worked out, perfect

Browser other questions tagged

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