freeCodeCamp.org


    Make a Person

    Fill in the object constructor with the following methods below:

    1. getFirstName()
    2. getLastName()
    3. getFullName()
    4. setFirstName(first)
    5. setLastName(last)
    6. 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.

    freeCodeCamp Challenge Guide: Make a Person

    1. var Person = function (firstAndLast) {
    2. // Only change code below this line
    3. // Complete the method below and implement the others similarly
    4. this.getFullName = function () {
    5. return firstAndLast;
    6. };
    7. this.getLastName = function () {
    8. return firstAndLast.split(' ').slice()[1];
    9. };
    10. this.getFirstName = function () {
    11. return firstAndLast.split(' ').slice()[0];
    12. };
    13. this.setFirstName = function (first) {
    14. firstAndLast = first + ' ' + firstAndLast.split(' ').slice()[1]
    15. };
    16. this.setLastName = function (last) {
    17. firstAndLast = firstAndLast.split(' ').slice()[0] + " " + last
    18. };
    19. this.setFullName = function (full) {
    20. firstAndLast = full
    21. }
    22. return firstAndLast;
    23. };
    24. var bob = new Person('Bob Ross');
    25. console.log(
    26. bob.getFullName(),
    27. bob.getLastName(),
    28. bob.getFirstName(),
    29. bob.setFirstName("Haskell"),
    30. bob.getFullName()
    31. )