Trying to get this to work in 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);
Not sure why it isn’t working, but I feel like it is something to do with how I am referencing the variables in curResults
JavaScript’s multiplication operator is
*, notx.The
xin the following lines is giving you an “unexpected identifier” error:So change them to:
No, that part is fine. Though as an aside, there’s no point in assigning
this.resultsA = resultsA;(andresultsBandC) in yourResults()constructor if you then immediately overwrite those values in the three lines above.