Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
arguments-optional.english.md
binary-agents.english.md
convert-html-entities.english.md
diff-two-arrays.english.md
dna-pairing.english.md
drop-it.english.md
everything-be-true.english.md
make-a-person.english.md
map-the-debris.english.md
missing-letters.english.md
pig-latin.english.md
search-and-replace.english.md
seek-and-destroy.english.md
smallest-common-multiple.english.md
sorted-union.english.md
spinal-tap-case.english.md
steamroller.english.md
sum-all-numbers-in-a-range.english.md
sum-all-odd-fibonacci-numbers.english.md
sum-all-primes.english.md
wherefore-art-thou.english.md
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env

137 lines
4.0 KiB
Markdown
Raw Normal View History

---
id: a2f1d72d9b908d0bd72bb9f6
title: Make a Person
challengeType: 5
forumTopicId: 16020
---
## Description
<section id='description'>
Fill in the object constructor with the following methods below:
```js
getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)
```
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.
testString: assert.deepEqual(Object.keys(bob).length, 6);
- text: <code>bob instanceof Person</code> should return true.
testString: assert.deepEqual(bob instanceof Person, true);
- text: <code>bob.firstName</code> should return undefined.
testString: assert.deepEqual(bob.firstName, undefined);
- text: <code>bob.lastName</code> should return undefined.
testString: assert.deepEqual(bob.lastName, undefined);
- text: <code>bob.getFirstName()</code> should return "Bob".
testString: assert.deepEqual(bob.getFirstName(), 'Bob');
- text: <code>bob.getLastName()</code> should return "Ross".
testString: assert.deepEqual(bob.getLastName(), 'Ross');
- text: <code>bob.getFullName()</code> should return "Bob Ross".
testString: assert.deepEqual(bob.getFullName(), 'Bob Ross');
- text: <code>bob.getFullName()</code> should return "Haskell Ross" after <code>bob.setFirstName("Haskell")</code>.
testString: assert.strictEqual((function () { bob.setFirstName("Haskell"); return bob.getFullName(); })(), 'Haskell Ross');
- text: <code>bob.getFullName()</code> should return "Haskell Curry" after <code>bob.setLastName("Curry")</code>.
testString: assert.strictEqual((function () { var _bob=new Person('Haskell Ross'); _bob.setLastName("Curry"); return _bob.getFullName(); })(), 'Haskell Curry');
- text: <code>bob.getFullName()</code> should return "Haskell Curry" after <code>bob.setFullName("Haskell Curry")</code>.
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFullName(); })(), 'Haskell Curry');
- text: <code>bob.getFirstName()</code> should return "Haskell" after <code>bob.setFullName("Haskell Curry")</code>.
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFirstName(); })(), 'Haskell');
- text: <code>bob.getLastName()</code> should return "Curry" after <code>bob.setFullName("Haskell Curry")</code>.
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getLastName(); })(), 'Curry');
```
</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;
function updateName(str) {
firstName = str.split(" ")[0];
lastName = str.split(" ")[1];
}
updateName(firstAndLast);
this.getFirstName = function(){
return firstName;
};
this.getLastName = function(){
return lastName;
};
this.getFullName = function(){
return firstName + " " + lastName;
};
this.setFirstName = function(str){
firstName = str;
};
this.setLastName = function(str){
lastName = str;
};
this.setFullName = function(str){
updateName(str);
};
};
var bob = new Person('Bob Ross');
bob.getFullName();
```
</section>