0

I am working on a project for SkillCrush and am getting a "not defined" error, when I feel as though I've defined the variable at the top of the code. I 100% know I'm doing something wrong, but not sure what. Any suggestions?

var createPolitician = function (name)
{
    var politician = {}; //is this not defined here?
    politician.name = name;
    politician.electionResults = null;
    politician.totalVotes = 0;

    return politician;
};

var oscar = createPolitician("Oscar Jiminez");
var luke = createPolitician("Luke Spencer");

oscar.electionResults = [5, 1, 7, 2, 33, 6, 4, 2, 1, 14, 8, 3, 1, 11, 11, 0, 5, 3, 3, 3, 7, 4, 8, 9, 3, 7, 2, 2, 4, 2, 8, 3, 15, 15, 2, 12, 0, 4, 13, 1, 3, 2, 8, 21, 3, 2, 11, 1, 3, 7, 2];

luke.electionResults = [4, 2, 4, 4, 22, 3, 3, 1, 2, 15, 8, 1, 3, 9, 0, 6, 1, 5, 5, 1, 3, 7, 8, 1, 3, 3, 1, 3, 2, 2, 6, 2, 14, 0, 1, 6, 7, 3, 7, 3, 6, 1, 3, 17, 3, 1, 2, 11, 2, 3, 1];

oscar.electionResults[9] = 1;
luke.electionResults[9] = 28;

oscar.electionResults[4] = 17;
luke.electionResults[4] = 38;

oscar.electionResults[43] = 11;
luke.electionResults[43] = 27;

console.log(oscar.electionResults);
console.log(luke.electionResults);

politician.countTotalVotes = function() 
{
    this.totalVotes = 0;

    for (var i = 0; i < this.electionResults.length; i++);
    {
        this.totalVotes = this.totalVotes + this.electionResults[i];
    }
}

oscar.countTotalVotes();
luke.countTotalVotes();

console.log(oscar.totalVotes);
console.log(luke.totalVotes);

Error:

"error"
    "ReferenceError: politician is not defined
    at reloyur.js:32:1"
4
  • 3
    post the full error please Commented Jul 17, 2018 at 15:54
  • Sorry about that! "error" "ReferenceError: politician is not defined at reloyur.js:32:1" Commented Jul 17, 2018 at 15:56
  • 2
    The error occurs at politician.countTotalVotes = .... politician is a local variable to createPolitician(). Just because you called createPolitician() does not mean you can suddenly use politician as if it is a global variable. If you want to add functionality to the object, consider rewriting it as a class Politician and write a constructor instead of a static function. Commented Jul 17, 2018 at 15:57
  • Thank you Patrick, I'm going to try that! Commented Jul 17, 2018 at 16:00

6 Answers 6

1
 politician.countTotalVotes = ...

That won't work as politician only exists while you create luke or oscar (it then points to one of them). Instead you could do:

luke.countTotalVotes = oscar.countTotalVotes = function() { /*...* };

But for more politicians that gets a bit complicated. You could however just make a function that you pass a politician into:

function countTotalVotes(politician) {
  //...
}

countTotalVotes(luke);

Or you use the power of inheritance.

Sign up to request clarification or add additional context in comments.

Comments

1

As I suggested in comments, you can rewrite Politician as a class, which basically gives you a template for reusable functionality. If you're not familiar with the concept, a good google term would be "prototypal inheritance" for follow-up on the subject in general. For now, here's a rewrite of your script using a class:

class Politician {
  constructor (name) {
    this.name = name;
    this.electionResults = [];
  }

  // no need to even write a .countTotalVotes() method
  // just define .totalVotes as a getter that does the work
  get totalVotes () {
    var total = 0;

    for (var i = 0; i < this.electionResults.length; i++) {
      total += this.electionResults[i];
    }

    return total;
  }
}

var oscar = new Politician("Oscar Jiminez");
var luke = new Politician("Luke Spencer");

oscar.electionResults = [5, 1, 7, 2, 33, 6, 4, 2, 1, 14, 8, 3, 1, 11, 11, 0, 5, 3, 3, 3, 7, 4, 8, 9, 3, 7, 2, 2, 4, 2, 8, 3, 15, 15, 2, 12, 0, 4, 13, 1, 3, 2, 8, 21, 3, 2, 11, 1, 3, 7, 2];

luke.electionResults = [4, 2, 4, 4, 22, 3, 3, 1, 2, 15, 8, 1, 3, 9, 0, 6, 1, 5, 5, 1, 3, 7, 8, 1, 3, 3, 1, 3, 2, 2, 6, 2, 14, 0, 1, 6, 7, 3, 7, 3, 6, 1, 3, 17, 3, 1, 2, 11, 2, 3, 1];

oscar.electionResults[9] = 1;
luke.electionResults[9] = 28;

oscar.electionResults[4] = 17;
luke.electionResults[4] = 38;

oscar.electionResults[43] = 11;
luke.electionResults[43] = 27;

// console.log(oscar.electionResults);
// console.log(luke.electionResults);

// these are no longer required
// oscar.countTotalVotes();
// luke.countTotalVotes();

console.log(oscar.name, 'has', oscar.totalVotes, 'votes');
console.log(luke.name, 'has', luke.totalVotes, 'votes');

Comments

1

Move your countTotalVotes function into your create function:

var createPolitician = function (name)
{

  var politician = {}; 
  politician.name = name;
  // set to an empty array so this is defined
  politician.electionResults = []; 
  politician.totalVotes = 0;
  // here politician exists, add a function to the object that you can call later
  politician.countTotalVotes = function() 
  {
     this.totalVotes = 0;
     for (var i = 0; i < this.electionResults.length; i++)
     {
        this.totalVotes = this.totalVotes + this.electionResults[i];
      }
  }

  return politician;

};

Comments

0

I corrected your mistakes in your code. You could change your code like follows:

function createPolitician(name)
{
    var politician = 
    {
        name: name,
        electionResults: null,
        totalVotes: 0,

        countTotalVotes: function() 
        {
            //this.totalVotes = 0; <- irrelevant because you use it only one time
            for(var i = 0; i < this.electionResults.length; i++)//";" symbol  on the end is your mistake too
            {
                this.totalVotes += this.electionResults[i];
            }

        }
    };
    return politician;
};

var oscar = createPolitician("Oscar Jiminez");
var luke = createPolitician("Luke Spencer");

oscar.electionResults = [5, 1, 7, 2, 33, 6, 4, 2, 1, 14, 8, 3, 1, 11, 11, 0, 5, 3, 3, 3, 7, 4, 8, 9, 3, 7, 2, 2, 4, 2, 8, 3, 15, 15, 2, 12, 0, 4, 13, 1, 3, 2, 8, 21, 3, 2, 11, 1, 3, 7, 2];

luke.electionResults = [4, 2, 4, 4, 22, 3, 3, 1, 2, 15, 8, 1, 3, 9, 0, 6, 1, 5, 5, 1, 3, 7, 8, 1, 3, 3, 1, 3, 2, 2, 6, 2, 14, 0, 1, 6, 7, 3, 7, 3, 6, 1, 3, 17, 3, 1, 2, 11, 2, 3, 1];

oscar.electionResults[9] = 1;
luke.electionResults[9] = 28;

oscar.electionResults[4] = 17;
luke.electionResults[4] = 38;

oscar.electionResults[43] = 11;
luke.electionResults[43] = 27;

console.log(oscar.electionResults);
console.log(luke.electionResults);

oscar.countTotalVotes();
luke.countTotalVotes();

console.log(oscar.name, 'has', oscar.totalVotes, 'votes');
console.log(luke.name, 'has', luke.totalVotes, 'votes');

1 Comment

Ah! Thank you! I did, while I was playing around, realize I didn't need that ';'. Being new at this, I tend to add them too often. This was beautiful and exactly what I was looking for. Thank you so much!
0

The error message should be accompanied with a line and column number. In this case, the error is occurring on line 32:

politician.countTotalVotes = function() 

In the definition of the createPolitician function, you declared a variable named politician. var creates a variable that lives within the function it was declared in. If it is not inside of a function, it creates a global variable.

You left the function the variable was declared in, and thus it isn't usable anymore.

If you think about it, it'd be very ambiguous if you could re-use that politician variable later on. What would it refer to?

If you want to be able to call countTotalVotes() on the politicians, then Patrick's comment about using a Class is a good approach to take. You could also assign the function to the politician object that you return from createPolitician - this would give each instance of the politician its own copy of the function.

1 Comment

Okay this has all been very helpful. I've moved some things around and am still getting errors, so I've resorted to asking an instructor for help. I'll let you all know once I get it straightened out! Realizing that it was a local variable and not global helped me to wrap my head around it a little better, so thank you for pointing that out! I'm obviously VERY new to JavaScript and am having a lot of trouble with it right now so I really appreciate everyone's patience with me!
0

It's all about variable scope. Declare the politician object variable outside of the createPolitician function for you to access out of the function. It cannot be accessed out of its scope that is why you get an undefined error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.