How do i fill the object constructor with methods

Tell us what’s happening:
I am clueless on how to solve the problem. I’ve done RSA for the problem. And here is my last resort.
Below is my script. I tried to fill the object constructor with methods. I almost completed it. But i do not know what to do next!

Your code so far


var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
this.getFullName = function() {
  return "";
};
return firstAndLast;

 this.getFirstName = function() {
    return firstName;
  };
  this.getLastName = function() {
    return lastName;
  };

   this.setFirstName = function(first){
    firstName = first;
  };
  this.setLastName = function(last){
    lastName = last;
  };
};

var bob = new Person('Bob Ross');
bob.getFullName();
bob.getFirstName();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Make a Person

Link to the challenge:

hint: maybe you need a local variable to hold the name that you can return or update as needed

Do you have all the methods you need?

I have added a local variable but can’t pass the test. Please walk me through the process. I would be very grateful.

See the code below:

var Person = function(firstAndLast) {

  // Complete the method below and implement the others similarly

  

  var firstName = "Bob";

  var lastName = "Ross";

  this.getFullName = function() {

    return "";

  };

  return firstAndLast;

   this.getFirstName = function() {

     var firstName = "Bob";

      return firstName;

    };

    this.getLastName = function() {

      return lastName;

    };

     this.setFirstName = function(first){

      firstName = first;

    };

    this.setLastName = function(last){

      lastName = last;

    };

    this.setFullName = function(firstAndLast){

       var arr = firstAndLast.split(' ');

      var firstName = "Bob";

       };

};

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

bob.getFullName();

Yes. But I Am still confused on what to do.

your Person function has a parameter firstAndLast with value of the full name but you never use it

you can’t hardwrite the first name and last name variables, as those should be determined by the creation of the new object, like in this line:

whatever string is passed in, it should work.

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.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

You are missing a required method. Compare what you have to the list of requirements.

Here is what I’ve been able to decipher:

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  var arr = firstAndLast.split(' ');
  var firstName = arr[0];
  var lastName = arr[1];
  this.getFullName = function() {
    return "";
  };

  this.getFirstName = function() {
      return firstName;
    };
    this.getLastName = function() {
      return lastName;
    };
    this.setFirstName = function(first){
      firstName = first;
    };
    this.setLastName = function(last){
      lastName = last;
    };
    this.setFullName = function(firstAndLast){
      var arr = firstAndLast.split(' ');
      firstName = arr[0];
      lastName = arr[1];
       };
  return firstAndLast;
};

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

I still get an error message. Please, what did i get wrong? Thanks for your patience!

This function is returning an empty string:

this.getFullName = function() {
    return "";
  };

So calling it will always return an empty string, not the person’s full name

> bob.getFullName()
""

Also, minor, but you don’t return in a constructor, so remove this line:

return firstAndLast;

As it is, when new is used, it’s going to ignore it anyway, but the function creates an object, you do not want it to return anything.

Thanks for your insights. I’ve learned a lot!

You took your time to explain concepts that are the heart and center of javaScript. Thank you!

1 Like