Skip to content

Instantly share code, notes, and snippets.

@amandarfernandes
Created December 13, 2017 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amandarfernandes/56d875851829fcd76aa60c71078c1229 to your computer and use it in GitHub Desktop.
Save amandarfernandes/56d875851829fcd76aa60c71078c1229 to your computer and use it in GitHub Desktop.
JsPrototypeExercises
// 1 - Create a constructor function for a Person. Each person should have a firstName, lastName, favoriteColor, favoriteNumber)
function Person(firstName, lastName, favoriteColor, favoriteNumber) {
this.firstName = firstName;
this.lastName = lastName,
this.favoriteColor = favoriteColor;
this.favoriteNumber = favoriteNumber;
this.family=[];
}
/* 2 - Add a function on the Person.prototype called fullName that returns the firstName and lastName property of an object created by the Person constructor concatenated together.
Examples:
var person = new Person("Elie", "Schoppik", "purple", 34)
person.fullName() // "Elie Schoppik"
*/
Person.prototype.fullName=function() {
return this.firstName+' '+this.lastName;
}
// 3 - Add a property on the object created from the Person function called family which is an empty array. This will involve you adding an additional line of code to your Person constructor.
/* 4 - Add a function on the Person.prototype called addToFamily which adds an object constructed from the Person constructor to the family array. To make sure that the object you are adding is an object construced from the Person constructor (HINT - take a look at the instanceof keyword). Make sure that your family array does not include duplicates! This method should return the length of the family array.
Examples:
var person = new Person("Elie", "Schoppik", "purple", 34)
var anotherPerson = new Person()
person.addToFamily(anotherPerson); // 1
person.addToFamily(anotherPerson); // 1
person.family.length // 1
person.addToFamily("test"); // 1
person.addToFamily({}); // 1
person.addToFamily([]); // 1
person.addToFamily(false); // 1
person.family.length // 1
*/
Person.prototype.addToFamily = function(person){
if(this.family.indexOf(person) === -1 && person instanceof Person){
this.family.push(person)
}
return this.family.length;
}
// PART II
// 1 - Implement your own version of Array.prototype.map. The function should accept a callback and return a new array with the result of the callback for each value in the array.
Array.prototype.map = function(fn) {
//console.log(this);console.log(fn);
var arr=[];
for (var i=0;i<this.length;i++) {
arr.push(fn(this[i],i,this));
}
return arr;
}
/* 2 - Implement a function called reverse that reverses a string and place it on the String.prototype
Examples:
"test".reverse() // "tset"
"tacocat".reverse() // "tacocat"
*/
String.prototype.reverse=function() {
return [].slice.call(this).reverse().join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment