Make a Person - error

Make a Person - error
0

#1

Tell us what’s happening:
Hello everyone,
The code below seem to work in a browser with no errors but the emulator here tells me: “TypeError: Cannot set property ‘firstName’ of undefined” - why?

Your code so far

var Person = function(firstAndLast) {
    const argsFromArray = Array.from(arguments)
    this.firstName = ''
    this.lastName = ''
    var fullName = this.firstName + ' ' + this.lastName
    
    function checkArgs (argsArrayToCheck) {
        if (argsArrayToCheck.length > 1) {
            console.log('More than 1 argument provided, stopping here.', argsArrayToCheck)
            return false
        } else if (typeof(argsArrayToCheck[0]) !== 'string') {
            console.log('You did not provide a string as argument, argument is a:', typeof(argsArrayToCheck[0]))
            return false
        } else {
            nameArray = argsArrayToCheck[0].split(' ')
            console.log('Valid argument provided')
            this.firstName = nameArray[0]
            this.lastName = nameArray[1]
            return this.firstName, this.lastName, true
        }
    }

    if (checkArgs(argsFromArray)) { // if the arguments provided are OK
        this.getFullName = function() {
            var fullName = firstName + ' ' + lastName
            console.log('Full name:', fullName)
            return fullName
        }
    
        this.getFirstName = function() {
            console.log('First name:', firstName)
            return firstName
        }
    
        this.getLastName = function() {
            console.log('Last name:', lastName)
            return lastName
        }

        this.setFirstName = function(first) {
            firstName = first
            console.log('New first name:', firstName)
            return firstName
        }

        this.setLastName = function(last) {
            lastName = last
            console.log('New last name:', lastName)
            return lastName
        }
    }
    
    this.setFullName = function(firstAndLast) {
        const newArgsFromArray = Array.from(arguments)
        
        if (checkArgs(newArgsFromArray)) { // if the new arguments provided are OK
            this.setFirstName = function(first) {
                firstName = first
                console.log('New first name:', firstName)
                return firstName
            }
    
            this.setLastName = function(last) {
                lastName = last
                console.log('New last name:', lastName)
                return lastName
            }

            var fullName = firstName + ' ' + lastName
            console.log('NEW Full name:', fullName)
            return fullName
        }
    }
    
};

var bob = new Person('Bob Ross');```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3256.0 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/make-a-person

#2

I think you are missing some code. I do not see:

var Person = function(firstAndLast) {

and there maybe some more code missing. Make sure all the code is showing and let us know.


#3

Weird, this is how the code was pasted automatically - i edited the original input, now everything is there.


#4

Nobody willing to help?


#5

Issues:

#1) You should only have 6 this methods/properties. You have 8.

#2) In general you are over complicating this challenge. I have commented out all the uneccessary parts of your solution and added 5 lines of code (see the two parts surround by to lines of multiple asterisks near the bottom of your solution). I have commented the additional lines of code.

var Person = function(firstAndLast) {
    // const argsFromArray = Array.from(arguments)
    // this.firstName = ''
    // this.lastName = ''
    // var fullName = this.firstName + ' ' + this.lastName
    
    // function checkArgs (argsArrayToCheck) {
    //     if (argsArrayToCheck.length > 1) {
    //         console.log('More than 1 argument provided, stopping here.', argsArrayToCheck)
    //         return false
    //     } else if (typeof(argsArrayToCheck[0]) !== 'string') {
    //         console.log('You did not provide a string as argument, argument is a:', typeof(argsArrayToCheck[0]))
    //         return false
    //     } else {
    //         nameArray = argsArrayToCheck[0].split(' ')
    //         console.log('Valid argument provided')
    //         this.firstName = nameArray[0]
    //         this.lastName = nameArray[1]
    //         return this.firstName, this.lastName, true
    //     }
    // }

    // if (checkArgs(argsFromArray)) { // if the arguments provided are OK
        this.getFullName = function() {
            var fullName = firstName + ' ' + lastName
            console.log('Full name:', fullName)
            return fullName
        }
    
        this.getFirstName = function() {
            console.log('First name:', firstName)
            return firstName
        }
    
        this.getLastName = function() {
            console.log('Last name:', lastName)
            return lastName
        }

        this.setFirstName = function(first) {
            firstName = first
            console.log('New first name:', firstName)
            // return firstName
        }

        this.setLastName = function(last) {
            lastName = last
            console.log('New last name:', lastName)
            // return lastName
        }
    // }
    
    this.setFullName = function(firstAndLast) {
        // const newArgsFromArray = Array.from(arguments)
        
        // if (checkArgs(newArgsFromArray)) { // if the new arguments provided are OK
            // this.setFirstName = function(first) {
            //     firstName = first
            //     console.log('New first name:', firstName)
            //     return firstName
            // }
    
            // this.setLastName = function(last) {
            //     lastName = last
            //     console.log('New last name:', lastName)
            //     return lastName
            // }

            // var fullName = firstName + ' ' + lastName
            // console.log('NEW Full name:', fullName)
            // return fullName
        // }
        
        // ***********************************************        
        // ***********************************************
        // need to separate the firstAndLast variable into 
        // an array in order to set corresponding firs and last names variables
        nameArray = firstAndLast.split(' ');
        firstName = nameArray[0];
        lastName = nameArray[1];
        // ***********************************************   
        // ***********************************************        
    }

    // ***********************************************      
    // ***********************************************    
    var firstName, lastName; // just need these two function variables to build everything else
    this.setFullName(firstAndLast); // this populates firstName and lastName
    // ***********************************************      
    // ***********************************************        
};

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

#6

Thank you @RandellDawson - apologies for late reply. I will have another go at it.


#7

It’s amazing how big of a difference makes taking a break:slight_smile:

var Person = function(firstAndLast) {
    // Complete the method below and implement the others similarly
    var firstName, lastName 
    var nameArray = firstAndLast.split(' ')
    firstName = nameArray[0]
    lastName= nameArray[1]

    this.getFullName = function() {
        var fullName = firstName + ' ' + lastName
        console.log(firstName + ' ' + lastName)
        return fullName
    };
    this.getFirstName = function() {
        console.log(firstName)
        return firstName
    };
    this.getLastName = function() {
        console.log(lastName)
        return lastName
    };
    this.setFirstName = function(first) {
        firstName = first
        return firstName
    };
    this.setLastName = function(last) {
        lastName = last
        return lastName
    };
    this.setFullName = function(firstAndLast) {
        nameArray = firstAndLast.split(' ')
        firstName = nameArray[0]
        lastName= nameArray[1]
    };
};