Created
June 15, 2018 21:34
-
-
Save eday69/25351887964a40d95f7396e1f1d4dcfb to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediate Algorithm Scripting: Make a Person
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill in the object constructor with the following methods below: | |
// 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. | |
var Person = function(firstAndLast) { | |
// Complete the method below and implement the others similarly | |
let fullName=firstAndLast | |
let full=fullName.split(" "); | |
this.getFirstName = function() { | |
return full[0]; | |
}; | |
this.getLastName = function() { | |
return full[1]; | |
}; | |
this.setFirstName = function(newName) { | |
full[0] = newName; | |
}; | |
this.setLastName = function(newLast) { | |
full[1] = newLast; | |
}; | |
this.getFullName = function() { | |
return this.getFirstName() + " " + this.getLastName(); | |
}; | |
this.setFullName = function(firstAndLast) { | |
fullName=firstAndLast; | |
full=fullName.split(" "); | |
}; | |
}; | |
var bob = new Person('Bob Ross'); | |
bob.getFullName(); | |
Object.keys(bob).length; // 6. | |
bob instanceof Person; // true. | |
bob.firstName; // undefined. | |
bob.lastName; // undefined. | |
bob.getFirstName(); // "Bob". | |
bob.getLastName(); // "Ross". | |
bob.getFullName(); // "Bob Ross". | |
bob.getFullName(); // "Haskell Ross" after bob.setFirstName("Haskell"). | |
bob.getFullName(); // "Haskell Curry" after bob.setLastName("Curry"). | |
bob.getFullName(); // "Haskell Curry" after bob.setFullName("Haskell Curry"). | |
bob.getFirstName(); // "Haskell" after bob.setFullName("Haskell Curry"). | |
bob.getLastName(); // "Curry" after bob.setFullName("Haskell Curry"). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// running tests
bob.getFirstName() should return "Bob".
bob.getLastName() should return "Ross".
bob.getFullName() should return "Bob Ross".
bob.getFullName() should return "Haskell Ross" after bob.setFirstName("Haskell").
// tests completed