WEIRD behaviour of Make a Person algorithm challenge

WEIRD behaviour of Make a Person algorithm challenge
0.0 0

#1

Problem

Hey, I’m seeing some weird stuff here. I’m working on Make a Person challenge and tests doesn’t let me finish although I have it all right. :grimacing:

If I ask for
bob.setFirstName(“Haskell”);
bob.getFullName();
it gives me that all is right and return “Haskell Ross” like it shoud. But if I just ask for “bob.getFullName();” (without setting a name) it suddenly become incorrect (but it shows the name like it should!!!) I’ve checked via external code editor - and it works fine. Could someone please explain what’s going on?:anguished:

Code

var Person = function(firstAndLast) {
  var memory = [];
    this.getFullName = function() {
      if (memory.length === 0) {
        memory.push(firstAndLast);
        return memory.join();
      }
      return memory[0];
    };
  this.getFirstName = function() {
     return memory[0].split(' ')[0];
  };
  this.getLastName = function() {
    return memory[0].split(' ')[1];
  };
  this.setFirstName = function(firstName) {
    memory.push(firstName +' '+ firstAndLast.split(' ')[1]);
  };
  this.setLastName = function (lastName) {
     memory.push(firstAndLast.split(' ')[0] + ' ' +lastName);
  };
  this.setFullName = function (fullName) {
    memory.push(fullName);
  };
  return firstAndLast;
};

var bob = new Person('Bob Ross');
bob.setFirstName("Haskell");  //▾▾▾
bob.getFullName();           // they're working, but the test denies it! 


#2

You code doesn’t actually work. What’s happening is that you’re pushing the correct name into your memory array when you call bob.setFirstName, so when getFullName is being called it returns the first item in the array. When you don’t call setFirstName, memory is an empty array and your function pushes, then returns, the parameter firstAndLast. Your code fails the tests because your other functions depend on the correct name being in the first index of memory, but once that’s set, it doesn’t change. Your code will actually crash if you try to run getFirstName or getLastName before a name is set because there’s nothing in memory.

Think of a way to do this without using an array. Don’t overcomplicate the solution; the simpler the better.