Make a Person: Why copy firstAndLast?

Tell us what’s happening:
Describe your issue in detail here.

Hello, I completed the “Make a Person” challenge with the code below.

However the solution copies the function parameter “firstAndLast” at the top of the function with

 let fullName = firstAndLast;

then uses that instead of “firstAndLast” as I did in my code.

Why make the copy? Seems to work the same without it. What am I missing?

Thank you!

  **Your code so far**

const Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  
  this.getFullName = () => firstAndLast;
  this.getFirstName = () =>  firstAndLast.split(" ")[0];
  this.getLastName = () =>  firstAndLast.split(" ")[1];

  this.setFirstName = x => {
      firstAndLast = x.split(" ")[0] + " " + firstAndLast.split(" ")[1]  
  }
  this.setLastName = y => {
      firstAndLast = firstAndLast.split(" ")[0] + " " + y;  
  } 
  this.setFullName = z => {
      firstAndLast = z.split(" ")[0] + " " + z.split(" ")[1];
  }
};

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

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

Challenge: Make a Person

Link to the challenge:

Because the function reassigns or mutates the value.

I would consider leaving function parameters alone to be a best practice, reassign whatever new values you need to new variables. Consider a larger function or more complex function, mutations and reassignments can get confusing quickly if not done carefully.

If the new value is different, the variable name should also reflect that. Good variable names help document the code.

1 Like

Makes sense! Wouldn’t have guessed it either. Thank you for the reply.

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