Intermediate Algorithm Scripting: Make a Person - what's the problem with my code?

Tell us what’s happening:

My code seems to work as intended. However, the test won’t pass at the line bob.getFirstName() should return “Haskell” after bob.setFullName("Haskell Curry")

I’ve manually tested my code to see if there’s anything wrong but everything works as it should. Am I doing something wrong or is it just some kind of bug? Help please. :sob:

Your code so far


var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  var nameArr = firstAndLast.split(' ');
  
  this.setFirstName = (first) => nameArr[0] = first;
  this.setLastName = (last) => nameArr[1] = last;
  this.setFullName = (firstLast) => {
    nameArr[0] = firstLast.match(/\w+/);
    nameArr[1] = firstLast.substring(firstLast.indexOf(" ") + 1);
  }

  this.getFirstName = () => nameArr[0];
  this.getLastName = () => nameArr[1];
  this.getFullName = () => `${nameArr[0]} ${nameArr[1]}`
  
};

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







Your browser information:

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

Link to the challenge:

The problem is with your setFullname function. You should log out nameArr at the end of that function and you might see what’s going on.

  this.setFullName = (firstLast) => {
    nameArr[0] = firstLast.match(/\w+/);
    nameArr[1] = firstLast.substring(firstLast.indexOf(" ") + 1);
    console.log(nameArr);
  }

Finally solved the problem by replacing my match method with a substring.

nameArr[0] = firstLast.substring(0, firstLast.indexOf(" "));

Not sure why it won’t accept my match regex though, because they pretty much do the exact same thing.

string.match returns an Array of matches. You are setting the first name to an array that has the result in it.
to make it work with match you need to set the firstName to the first (and only) result of match.

nameArr[0] = firstLast.match(/\w+/)[0];

I see. All this time I though match returns a string. Thanks for clearing that up! :grinning: