Intermediate Algorithm Scripting - Make a Person - can't use Array methods

Tell us what’s happening:
I don’t understand why:

  1. my .split, .splice and .join methods don’t work here… I’m getting weird results with .splice not working at all as expected.

  2. Why do I have to declare and store firstAndLast value in a variable and can’t just use firstAndLast as it is ? I can see it’s stored in created object, so why can’t use it, overwrite it etc ?

Your code so far

//THIS DOEN"T WORK
const Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  // let fullName = firstAndLast

  this.getFullName = function() {
    return firstAndLast; 
  };
  this.getFirstName = function(){
    return firstAndLast.split(" ")[0]
  }
  this.getLastName = function(){
    return firstAndLast.split(" ")[1]
  }
  //PROBLEMATIC AREA ------------------------------
  this.setFirstName = function(first){ 
    // firstAndLast = first + " " + firstAndLast.split(" ")[1]
    firstAndLast = firstAndLast.split(" ").splice(0,1,first).join(" ")
    console.log(firstAndLast)
  }
  //PROBLEMATIC AREA ------------------------------
};

const bob = new Person('Bob Ross');
bob.setFirstName("Haskell")
console.log(bob.getFirstName()); 

//THIS DOES WORK

const Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly
  // let fullName = firstAndLast

  this.getFullName = function() {
    return firstAndLast; 
  };
  this.getFirstName = function(){
    return firstAndLast.split(" ")[0]
  }
  this.getLastName = function(){
    return firstAndLast.split(" ")[1]
  }
  //PROBLEMATIC AREA ------------------------------
  this.setFirstName = function(first){ 
    firstAndLast = first + " " + firstAndLast.split(" ")[1]
    console.log(firstAndLast)
  }
 //PROBLEMATIC AREA ------------------------------
};

const bob = new Person('Bob Ross');
bob.setFirstName("Haskell")
console.log(bob.getFirstName()); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0

Challenge: Intermediate Algorithm Scripting - Make a Person

Link to the challenge:

Because that is bad practice. If I give you a string, I don’t want you to modify my string unless you expressly state that is what you will do.

These methods can be used but I woudn’t use splice here

Maybe they can be used - but they don’t work here.
I asked the second question because I don’t understand why they don’t work.
The second part of code does work, and I can complete the challenge - no problem. But I’d like to know why most of the Array methods don’t work here - what is the reason.

All array methods work per their design. Nothing stops array methods from functioning here.

You aren’t respecting the return value of splice here.

My mistake.
I thought .splice returns the modified array, like .slice and treated it like that.
All clear, thank You for Your help.

1 Like