Window error in JavaScript

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()

{
  this.totalResults = 0;
  
  {;window.runnerWindow.protect.protect({ line: 22, reset: true }); for(var i=0; i`

Then further down the script, a winner needs to be declared. I think the error are related but I’m not sure.

Hi @camie.cam.codes!

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Syntax errors and logic issue:

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.

  1. 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.

  2. Use the constructor function and pass in all the data it needs on initialization (like the array).

  3. 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.

1 Like

Thank you very much for your help. It is a lot easier to understand OOP now.

Did you get it working?

If you need more help just ask, you can always post a link to your new code.

1 Like

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.

1 Like

You mean the if logic for the winner?

Whichever one has the most total votes should win, so it seems fairly straightforward. What about it confuses you?

1 Like
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.