Make a Person - Algorithm Challenge

I’m doing the Make a Person challenge and just can’t debug my solution and understand where I’m going wrong. After some exhausting hours, I decided to check the spoiler alert :disappointed_relieved: I see its similar but they put the splitting of the full name in both the set and get methods. Why can’t I not have it as a local variable?

Here is my code:

    function Person(firstAndLast) {
      
        this.sFullName = firstAndLast;
        var aNames = firstAndLast.split(' ');
     
        this.sFirstName = aNames[0];
        this.sLastName = aNames[1];

      
        // Setters
        this.setFirstName = function(sFirstName){
            this.sFirstName = sFirstName;
        };
        this.setLastName = function(sLastName){
            this.sLastName = sLastName;
        };
        this.setFullName = function(sFullName){
            this.sFullName = sFullName;
        };
        
      
        // Getters
        this.getFirstName = function() {
            return this.sFirstName;
        }; 
        this.getLastName = function() {
            return this.sLastName;
        };
        this.getFullName = function() {
            return this.sFullName;
        }; 
        
    }

    var bob = new Person('Bob Ross');
1 Like

Too many keys.

let bob = new Person("Bob Ross");
console.log(Object.keys(bob).length) // 9 should be 6!
    function Person(firstAndLast) {
      
        this.sFullName = firstAndLast; // 1   Assigning firstAndLast to key sFullName of this.
        var aNames = firstAndLast.split(' '); // local variable in scope Person doesn't count as key but can be accessed by functions through closure.
     
        this.sFirstName = aNames[0]; // 2
        this.sLastName = aNames[1];// 3

      
        // Setters
        this.setFirstName = function(sFirstName){ // 4
            this.sFirstName = sFirstName;
        };
        this.setLastName = function(sLastName){ // 5
            this.sLastName = sLastName;
        };
        this.setFullName = function(sFullName){ // 6
            this.sFullName = sFullName;
        };
        
      
        // Getters
        this.getFirstName = function() { // 7
            return this.sFirstName;
        }; 
        this.getLastName = function() { // 8
            return this.sLastName;
        };
        this.getFullName = function() { // 9
            return this.sFullName;
        }; 
        
    }

They want you to learn how closure works.

1 Like

Oh ok. Yea, I didn’t get the key part they were asking. Thanks for clearing things up.