2018-10-04 14:37:37 +01:00
---
id: a2f1d72d9b908d0bd72bb9f6
title: Make a Person
challengeType: 5
---
## Description
< section id = 'description' >
Fill in the object constructor with the following methods below:
2019-05-17 06:20:30 -07:00
```js
getFirstName()
2018-10-04 14:37:37 +01:00
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
2019-05-17 06:20:30 -07:00
setFullName(firstAndLast)
```
2018-10-04 14:37:37 +01:00
Run the tests to see the expected output for each method.
The methods that take an argument must accept only one argument and it has to be a string.
These methods must be the only available means of interacting with the object.
Remember to use < a href = 'http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target = '_blank' > Read-Search-Ask< / a > if you get stuck. Try to pair program. Write your own code.
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
- text: < code > Object.keys(bob).length</ code > should return 6.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(Object.keys(bob).length, 6);
2018-10-04 14:37:37 +01:00
- text: < code > bob instanceof Person</ code > should return true.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(bob instanceof Person, true);
2018-10-04 14:37:37 +01:00
- text: < code > bob.firstName</ code > should return undefined.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(bob.firstName, undefined);
2018-10-04 14:37:37 +01:00
- text: < code > bob.lastName</ code > should return undefined.
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(bob.lastName, undefined);
2018-10-04 14:37:37 +01:00
- text: < code > bob.getFirstName()</ code > should return "Bob".
2019-07-27 21:16:04 -07:00
testString: assert.deepEqual(bob.getFirstName(), 'Bob');
2018-10-04 14:37:37 +01:00
- text: < code > bob.getLastName()</ code > should return "Ross".
2019-07-27 21:16:04 -07:00
testString: assert.deepEqual(bob.getLastName(), 'Ross');
2018-10-04 14:37:37 +01:00
- text: < code > bob.getFullName()</ code > should return "Bob Ross".
2019-07-27 21:16:04 -07:00
testString: assert.deepEqual(bob.getFullName(), 'Bob Ross');
2018-10-04 14:37:37 +01:00
- text: < code > bob.getFullName()</ code > should return "Haskell Ross" after < code > bob.setFirstName("Haskell")</ code > .
2019-07-27 21:16:04 -07:00
testString: assert.strictEqual((function () { bob.setFirstName("Haskell"); return bob.getFullName(); })(), 'Haskell Ross');
2018-10-04 14:37:37 +01:00
- text: < code > bob.getFullName()</ code > should return "Haskell Curry" after < code > bob.setLastName("Curry")</ code > .
2019-07-27 21:16:04 -07:00
testString: assert.strictEqual((function () { var _bob=new Person('Haskell Ross'); _bob.setLastName("Curry"); return _bob.getFullName(); })(), 'Haskell Curry');
2018-10-04 14:37:37 +01:00
- text: < code > bob.getFullName()</ code > should return "Haskell Curry" after < code > bob.setFullName("Haskell Curry")</ code > .
2019-07-27 21:16:04 -07:00
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFullName(); })(), 'Haskell Curry');
2018-10-04 14:37:37 +01:00
- text: < code > bob.getFirstName()</ code > should return "Haskell" after < code > bob.setFullName("Haskell Curry")</ code > .
2019-07-27 21:16:04 -07:00
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFirstName(); })(), 'Haskell');
2018-10-04 14:37:37 +01:00
- text: < code > bob.getLastName()</ code > should return "Curry" after < code > bob.setFullName("Haskell Curry")</ code > .
2019-07-27 21:16:04 -07:00
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getLastName(); })(), 'Curry');
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
this.getFullName = function() {
return "";
};
return firstAndLast;
};
var bob = new Person('Bob Ross');
bob.getFullName();
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
var Person = function(firstAndLast) {
var firstName, lastName;
2018-10-08 01:01:53 +01:00
function updateName(str) {
2018-10-04 14:37:37 +01:00
firstName = str.split(" ")[0];
2018-10-08 01:01:53 +01:00
lastName = str.split(" ")[1];
2018-10-04 14:37:37 +01:00
}
updateName(firstAndLast);
this.getFirstName = function(){
return firstName;
};
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
this.getLastName = function(){
return lastName;
};
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
this.getFullName = function(){
return firstName + " " + lastName;
};
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
this.setFirstName = function(str){
firstName = str;
};
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
this.setLastName = function(str){
lastName = str;
};
2018-10-08 01:01:53 +01:00
2018-10-04 14:37:37 +01:00
this.setFullName = function(str){
updateName(str);
};
};
var bob = new Person('Bob Ross');
bob.getFullName();
```
< / section >