🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
When I started the program I figured I just had to create the six functions mentioned in the details. However, it was not that 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 in 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() {/*...*/}
try to solve the problem now
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;
try to solve the problem now
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.
try to solve the problem now
Solution ahead!
var Person = function(firstAndLast) {
var fullName = firstAndLast;
this.getFirstName = function() {
return fullName.split(" ")[0];
};
this.getLastName = function() {
return fullName.split(" ")[1];
};
this.getFullName = function() {
return fullName;
};
this.setFirstName = function(name) {
fullName = name + " " + fullName.split(" ")[1];
};
this.setLastName = function(name) {
fullName = fullName.split(" ")[0] + " " + name;
};
this.setFullName = function(name) {
fullName = name;
};
};
var bob = new Person('Bob Ross');
bob.getFullName();
🚀 Run Code
- Create a variable that will make a copy of the full name that was passed as a parameter.
- Then we can proceed to create the six methods needed and return what is asked for.
- For the individual setters, we can use the split to turn the fullname into an array of first and last names and concatenate the unchanged portion of the name with what was passed as a parameter.
If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
Thanks @Rafase282 @ltegman for your help with Algorithm: Make a Person
⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.