Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person

Tell us what’s happening:

How may I set the new properties in the object?

ob.getFullName() should return “Haskell Ross” after bob.setFirstName("Haskell") .

bob.getFullName() should return “Haskell Curry” after bob.setLastName("Curry") .

bob.getFullName() should return “Haskell Curry” after bob.setFullName("Haskell Curry") .

bob.getFirstName() should return “Haskell” after bob.setFullName("Haskell Curry") .

bob.getLastName() should return “Curry” after bob.setFullName("Haskell Curry")

Your code so far


var Person = function(firstAndLast) {
 // Complete the method below and implement the others similarly




 this.getFullName = function() {

   return "Bob Ross";
 };

this.getFirstName = function() {

   return "Bob";
 };

 this.getLastName = function() {

   return "Ross";
 };

 this.setFullName = function(firstAndLast) {

   return "Bob Ross";
 };

 this.setFirstName = function(first) {

   return "Bob Ross";
 };
 
 this.setLastName = function(last) {

   return "Bob Ross";
 };
};
console.log(bob)
var bob = new Person('Bob Ross');
bob.getFullName();
bob.getFirstName();
bob.getLastName();
bob.setFullName();
bob.setFirstName("Haskell");
bob.setLastName();
/*Intermediate Algorithm Scripting: Make a Person

Fill in the object constructor with the following methods below:

getFirstName()

getLastName()

getFullName()

setFirstName(first)

setLastName(last)

setFullName(firstAndLast)

Run the tests to see the expected output for each method. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the only available means of interacting with the object.

Object.keys(bob).length should return 6.

bob instanceof Person should return true.

bob.firstName should return undefined.

bob.lastName should return undefined.

bob.getFirstName() should return "Bob".

bob.getLastName() should return "Ross".

bob.getFullName() should return "Bob Ross".

bob.getFullName() should return "Haskell Ross" after 

bob.setFirstName("Haskell").

bob.getFullName() should return "Haskell Curry" after 

bob.setLastName("Curry").

bob.getFullName() should return "Haskell Curry" after 

bob.setFullName("Haskell Curry").

bob.getFirstName() should return "Haskell" after bob.

setFullName("Haskell Curry").

bob.getLastName() should return "Curry" after bob.

setFullName("Haskell Curry"). */

Your browser information:

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

Challenge: Make a Person

Link to the challenge:

Hey @santiagodeleondecar1,

On these types of problems (algorithm), you don’t typically want to return a plain string, you want it so that if you give it a different input, it will change itself.

1 Like

How may I set that? I am gwting Bob Ross when I should get Haskell Curry. It is not getting modified.

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  let fullName="Bob Ross"
  this.getFullName = function() {
    return fullName ;
  };
  let firstName="Bob"
  this.getFirstName = function() {
    return firstName ;
  }; 
  let lastName="Ross"
  this.getLastName = function() {
    return lastName ;
  }; 
  let setFullName="Haskell Curry"
  this.setFullName = function() {
    return setFullName   ;
  }; 
  let setFirstName="Haskell"
  this.setFirstName = function() {
    return setFirstName;
  }; 
let setLastName="Curry"
  this.setLastName = function() {
    return setLastName  ;
  }; 
 
};
 
var bob = new Person('Bob Ross');
console.log(bob.getFullName());//should print Haskell Curry bob.setFullName("Haskell Curry")
console.log(bob.setFullName())

/*Intermediate Algorithm Scripting: Make a Person
Fill in the object constructor with the following methods below:

getFirstName()

getLastName()

getFullName()

setFirstName(first)

setLastName(last)

setFullName(firstAndLast)

Run the tests to see the expected output for each method. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the only available means of interacting with the object.


Object.keys(bob).length should return 6.

bob instanceof Person should return true.

bob.firstName should return undefined.

bob.lastName should return undefined.

bob.getFirstName() should return "Bob".

bob.getLastName() should return "Ross".

bob.getFullName() should return "Bob Ross".

bob.getFullName() should return "Haskell Ross" after 

bob.setFirstName("Haskell").

bob.getFullName() should return "Haskell Curry" after 

bob.setLastName("Curry").

bob.getFullName() should return "Haskell Curry" after 

bob.setFullName("Haskell Curry").

bob.getFirstName() should return "Haskell" after bob.

setFullName("Haskell Curry").
bob.getLastName() should return "Curry" after bob.

setFullName("Haskell Curry").*/

you are always use strings and hardcoding values

you have function parameters to use to make the function reusable, you should use that

if you are assigning any string literal inside your function you are doing it wrong (in this case at least)

you have function parameters, for example firstAndLast

1 Like

anyway, the comment in the snippet above is not true

var bob = new Person("Bob Ross");
console.log(bob.getFullName()); // this should print "Bob Ross"
bob.setFullName("Haskell Curry");
console.log(bob.getFullName()); // this should print "Haskell Curry"
1 Like

I reused the function but I do not know why it does not comply with the next tasks:
bob.getFullName() should return “Haskell Ross” after bob.setFirstName("Haskell") .

bob.getFullName() should return “Haskell Curry” after bob.setLastName("Curry") .

bob.getFullName() should return “Haskell Curry” after bob.setFullName("Haskell Curry") .

bob.getFirstName() should return “Haskell” after bob.setFullName("Haskell Curry") .

bob.getLastName() should return “Curry” after bob.setFullName("Haskell Curry") .

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  this.getFullName = function() {
    return firstAndLast.split(" ")[0]+" "+firstAndLast.split(" ")[1] ;   
  };
  this.getFirstName = function() {
    return firstAndLast.split(" ")[0] ;
  };
  this.getLastName = function() {
    return firstAndLast.split(" ")[1] ;
  };
  this.setFullName = function() {
    return  firstAndLast.split(" ")[0]+" "+firstAndLast.split(" ")[1]  ;
  };
  this.setFirstName = function() {
    return firstAndLast.split(" ")[0] ;
  };
  this.setLastName = function() {
     return firstAndLast.split(" ")[1];
  };
};

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

your setFullName and getFullName do the exact same thing

setFullName should give a new name to your person, it shouldn’t do the exact same thing of the other one

1 Like

It is done! Thanks for your time!

var Person = function(firstAndLast) {

  // Complete the method below and implement the others similarly

  this.getFirstName = function() {

    return firstAndLast.split(" ")[0] 

  };

  this.getLastName = function() {

    return  firstAndLast.split(" ")[1] 

  };

  this.getFullName = function() {

    return firstAndLast 

  };

  this.setFirstName = function(first) {

    

    return firstAndLast=first + " "+ this.getLastName()//I am getting here Haskell Ross, what should I change to get "Haskell Ross" when I use bob.getFullName() 

  };

  this.setLastName = function(last) {

    

    return firstAndLast= this.getFirstName() + " "+ last 

  };

  

  this.setFullName = function(haskellCurry) {

    

     return firstAndLast=haskellCurry

  

  };

};

var bob = new Person('Bob Ross');
  this.setFullName = function(haskellCurry) {
     return firstAndLast=haskellCurry
  };

I would name this variable something other than haskellCurry… this function should (and does) work for any name.

Also, I am not sure why your set*Name functions are returning the validity check value for the assignment operator. Is that on purpose anything?

1 Like

I will change it, but I do not understand the second thing.

This is fun, I learned something today.

In any case, the set*Name functions aren’t really intended to return any value.

1 Like

That is an acute observation. I did not notice it. I just though how to update the values.