-
Notifications
You must be signed in to change notification settings - Fork 341
Bonfire Make a Person
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | My Original Wiki
- Difficulty: 3/5
Fill in the object constructor with the methods specified in the tests.
Those methods are:
- getFirstName()
- getLastName()
- getFullName()
- setFirstName(first)
- setLastName(last)
- setFullName(firstAndLast)
All functions that take an argument have an arity of 1, and the argument will be a string.
These methods must be the only available means for interacting with the object.
Remember to use RSAP if you get stuck. Try to pair program. Write your own code.
var Person = function(firstAndLast) {
return firstAndLast;
};
var bob = new Person('Bob Ross');
bob.getFullName();
When I started the program I figured I just had to create the six functions mentioned in the details. However, it was not as simple. Creating them as a function was not the right way, I had to create them in a different way to make them a key.
There is also a tricky part as you need six keys no more or less, so at first I had the variable that store the original name as a key too which was wrong.
As for the usage of array, that is optional, you could also create new variable to hold the separated string if you wish but an array is easier to deal with as strings are immutable.
Read the instructions carefully, it is always a good hint on itself to run the code and check what the test results were so you know what to expect but do not fixate yourself on that. Once you understand what you need to do, this problem is very easy and straightforward.
Use the this notation to create the keys instead of regular functions: This means instead of var varName = function() {/*...*/}
you should use this.varName = function() {/*...*/}
The program has a test that checks for how many keys you used, they have to be exactly six, the six mentioned in the details section. This means if you need to work with variables, make them local and not a key: this.fullName = firstAndLast;
Often the code would not work the way you expect it due to wrong variable names, make sure to check that you spell them the right way. This happens to all of us at some point.
Solution ahead!
Please try hard before you check this solution.
var Person = function(firstAndLast) {
var fullName = firstAndLast;
var arr = fullName.split(' ');
this.getFirstName = function() {
return arr[0];
};
this.getLastName = function() {
return arr[1];
};
this.getFullName = function() {
return fullName;
};
this.setFirstName = function(first) {
arr[0] = first;
fullName = arr.join(' ');
};
this.setLastName = function(last) {
arr[1] = last;
fullName = arr.join(' ');
};
this.setFullName = function(firstAndLast) {
fullName = firstAndLast;
arr = fullName.split(' ');
};
};
var bob = new Person('Bob Ross');
bob.getFullName();
- Create a variable that will make a copy of the full name that was passed as a parameter.
- Create another variable that will split that full name into first and last name array.
- Then we can proceed to create the six keys needed and return what is needed.
- For the setters, we can use the arr array and the right index to change the value to what was passed as a parameter.
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3