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"?
– Sergio
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 -.
– Cobra
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?
– Sergio
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!
– Cobra