试图让它在 JS 中工作:
var calculate = function(votesA, votesB, votesC) {
var total = votesA + votesB + votesC;
function Results(resultsA, resultsB, resultsC) {
this.resultsA = resultsA;
this.resultsB = resultsB;
this.resultsC = resultsC;
}
var curResults = new Results(votesA, votesB, votesC);
curResults.resultsA = (votesA / total) x 100;
curResults.resultsB = (votesB / total) x 100;
curResults.resultsC = (votesC / total) x 100;
console.log(curResults.resultsA, curResults.resultsB, curResults.resultsC);
}
calculate(5,4,8);
calculate(5,6,8);
calculate(6,8,9);
不知道为什么它不起作用,但我觉得这与我在 curResults 中引用变量的方式有关