Make a Person is correct in the browser's console, but accuses an error

Make a Person is correct in the browser's console, but accuses an error
0

#1

Have tested this code in Firefox, and all the test challenges do match in the browser’s console just fine.
The test errors in the last line of challenge, telling that (what appears in the console.log correctly) is expected?

Your code so far


var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
var init = firstAndLast.split(" ");

	this.setFullName = function(arg){
	init.splice(0,2);
	init.push(arg.split(" "));
	this.setFullName.kid = init[0]; 
	};
	
	this.setFirstName = function(first){
	
	if(!this.setFullName.kid){	
	init[0] = first;
	this.setFirstName.kid = init[0];

	}
	
	else{
	this.setFullName.kid[0] = first;	
	this.setFirstName.kid = this.setFullName.kid[0];
	}
	};
//--------------------------------------------------------------------
	this.setLastName = function(last){
	
	if(!this.setFullName.kid){	
	init[1] = last;
	this.setLastName.kid = init[1];
	}
	
	else{
	this.setFullName.kid[1] = last;	
	this.setLastName.kid = this.setFullName.kid[1];
	}
	};
	

	this.getFullName = function(){if(!this.setFullName.kid){
		return init.join(" ");
		}
		var exporty = this.setFullName.kid;
		return exporty.join(" ");
		};
 
	this.getFirstName = function(){if(!this.setFirstName.kid){
		return init[0];
		}
		return this.setFirstName.kid;
		};

        this.getLastName = function(){if(!this.setLastName.kid){
		return init[1];
		}	
		return this.setLastName.kid;
		};
};

var bob = new Person('Bob Ross');
console.log(bob.getFullName());
console.log(bob.getFirstName());
console.log(bob.getLastName());
console.log(bob.getFullName());
bob.setFirstName("Haskell");
console.log(bob.getFullName());
bob.setLastName("Curry");
console.log(bob.getFullName());
bob.setFullName("Haskell Curry");
console.log(bob.getFirstName());
bob.setFullName("Haskell Curry");
console.log(bob.getLastName());
// It errors this last line, but the browser displays accordingly.

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person


#2

The failing test is correct.


#3

Hello, what do you mean?


#4

The test is that after running bob.setFullName("Haskell Curry") then bob.getLastName() should return “Curry”. If Above is a live version of your code with the test condition at the end. If you run it you will see that bob.getLastName() is returning undefined.


#5

Why does the browser shows all lines correctly and in sequence?
var bob = new Person(‘Bob Ross’);
console.log(bob.getFullName());
console.log(bob.getFirstName());
console.log(bob.getLastName());
console.log(bob.getFullName());
bob.setFirstName(“Haskell”);
console.log(bob.getFullName());
bob.setLastName(“Curry”);
console.log(bob.getFullName());
bob.setFullName(“Haskell Curry”);
console.log(bob.getFirstName());
bob.setFullName(“Haskell Curry”);
console.log(bob.getLastName());
it only errors out the last one, meaning, the browser console.logs, all correctly ( and the test challenges states the last line is wrong, saying what is exepcted, is exactly what the browser displays?)
Bellow is the browser’s output:

13:43:47.387 Bob Ross Scratch20.html:71:1
13:43:47.387 Bob Scratch20.html:72:1
13:43:47.388 Ross Scratch20.html:73:1
13:43:47.388 Bob Ross Scratch20.html:74:1
13:43:47.388 Haskell Ross Scratch20.html:76:1
13:43:47.388 Haskell Curry Scratch20.html:78:1
13:43:47.389 Haskell Scratch20.html:80:1
13:43:47.389 Curry Scratch20.html:82:1


#6

Because when you do all of those in order, you have done setLastName("Curry"). But each test is a fresh start, so the only commands that have been run are creating bob, setFullName and getLastName.


#7

Thank you for your reply, will look further and revise the code, I just want to use an array to hold the strings, and work on the array, instead of manipulating the strings :slight_smile: