Here is my JS Bin. I am currently working on a mock election map. I am receiving an error after trying to get my total votes to tally. Can someone explain to me what this error means `function()
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
You have a semicolon after the parentheses on the for loop that shouldn’t be there.
totalVotesTally is a method, you can not compare methods.
You have this:
(jane.totalVotesTally < jill.totalVotesTally)
You can compare the values they return when you run them.
(jane.totalVotesTally() < jill.totalVotesTally())
However, your method is also not returning a value which it would have to (totalVotesTally needs to return totalResults).
Your if logic is backward, you are saying if jane is less than jill, jane won, and if jane is greater than jill, jill won. It should be the other way around.
If you really want to do this OOP style, I might suggest spending a little more time learning more about it.
I would suggest you set all the properties on this and initialize the instances using the new keyword. Or maybe even look into using Class syntax instead.
Use the constructor function and pass in all the data it needs on initialization (like the array).
If you do want to be able to set and get, I would use dedicated methods for this, e.g. for setting the array from outside on the instance after initialization, or changing individual vote counts at specific indexes.
Yes, I was able to get it resolved. I saw where I was calling the method in my method for declaring the winner instead of the totalVotes to declare the winner. To be honest I am still lost on why the comparative statement needs to be in the order that it is in the if/else. But it works now. So I’ll study more. Thank you again for all your help.
var winner="???";
if (jane.totalVotes > jill.totalVotes){
winner = jane.name;
}else if(jane.totalVotes < jill.totalVotes){
winner = jill.name;
}
else{
winner="Draw";
}
This is the correct solution. In the original script. I had both comparative statements going in the same direction. I think that was why it wasn’t working.