Intermediate Algorithm Scripting - Make a Person

Tell us what’s happening:
I found bits and pieces for the challenge by googling and stumbled through but the problem is that I don’t have the slightest idea how this constructors is supposed to be called to change the name from Bob Ross to anything else.
Neither do I understand how it does it in the set part.

The part with get is clear as it extracts the wanted part of the string given as argument but I can’t figure out how set part changes the name to something else.
I do understand all the set parts are functions that take an argument to process but how does any of those functions get the argument? How do I insert that argument into the constructor to make the “subfunctions” operate on the argument.

I have tried console.logging nearly everything that I came up with while also creating different function calls with different strings but I haven’t seen anything else been printed out than Bob Ross as stated in const bob = new Person(‘Bob Ross’);

Your code so far

const Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  let names = firstAndLast;
  
  this.getFirstName = function() {
    return names.split(" ")[0]
  }
  this.getLastName = function() {
    return names.split(" ")[1]
  }
  this.getFullName = function() {
    return names;
  }
  this.setFirstName = function(input) {
    names = input + " " + names.split(" ")[1]
  }
  this.setLastName = function(input) {
    names = names.split(" ")[0] + " " + input
  }
  this.setFullName = function(input) {
    names = input
  }
  return firstAndLast;
};

const bob = new Person('Bob Ross');
console.log(bob.getFullName());

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Make a Person

Link to the challenge:

I’m not exactly sure which part of the setter methods you aren’t understanding so I’ll try to cover everything with the hope that I cover what you need.

Each setter method takes a string argument. Let’s use setFullName as the example. You would call this method as:

const bob = new Person('Bob Ross');
bob.setFullName('Bob Roberts');

The second line changes the full name (stored internally in the names variable) from “Bob Ross” to “Bob Roberts”. This method accepts a string (named input in your code) and then sets the variable names to the string passed in through input. Now, if you were to call getFullName it would return the new name:

console.log(bob.getFullName()); // returns "Bob Roberts"

The other two setter methods work exactly the same way except they only modify either the first or last name.

I think I got the grasp of it. For some reason I was not able to figure out how to call for the setter or getter methods inside the object.

With your help and some trial and error it is now solved. Thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.