Chapter 8

Here are the solutions to the Exercises, for the Chapter 8, Using Inheritance chapter.

Exercise 1

 'use strict'​;
 
 class​ FunctionalSet ​extends​ Set {
  filter(predicate) {
 return​ ​new​ FunctionalSet([...​this​].filter(predicate));
  }
 
  map(mapper) {
 return​ ​new​ FunctionalSet([...​this​].map(mapper));
  }
  reduce(accumulator, identity) {
 return​ [...​this​].reduce(accumulator, identity);
  }
 }
 
 const​ ​set​ = ​new​ FunctionalSet([​'Jack'​, ​'Jill'​, ​'Tom'​, ​'Jerry'​]);
 
 const​ jSet = ​set​.filter(name => name.startsWith(​'J'​));
 const​ allCaps = ​set​.map(name => name.toUpperCase());
 
 const​ totalLengthOfJWords =
 set​.filter(name => name.startsWith(​'J'​))
  .reduce((total, word) => total + word.length, 0);
 
 console.log(jSet); ​//FunctionalSet { 'Jack', 'Jill', 'Jerry' }
 console.log(allCaps); ​//FunctionalSet { 'JACK', 'JILL', 'TOM', 'JERRY' }
 console.log(totalLengthOfJWords); ​//13

Exercise 2

No code change is necessary. The add() method of Set returns an instance based on the runtime type. When add() is called on an instance of FunctionalSet, the returned instance is of the same derived type.

Exercise 3

 'use strict'​;
 
 class​ BoundedSet ​extends​ Set {
 constructor​(capacity, initialValues) {
 super​();
 this​.capacity = capacity;
 
 if​(initialValues.length <= capacity) {
  initialValues.forEach(value => ​this​.add(value));
  }
  }
 
  add(value) {
 if​(​this​.has(value)) ​return​;
 
 if​(​this​.size < ​this​.capacity) {
 super​.add(value);
  } ​else​ {
 throw​ ​new​ Error(​`exceeded capacity of ​${​this​.capacity}​ elements`​);
  }
  }
 }
 
 const​ ​set​ = ​new​ BoundedSet(5, [​'Apple'​, ​'Banana'​, ​'Grape'​, ​'Mangoe'​]);
 
 set​.add(​'Orange'​);
 set​.add(​'Apple'​);
 try​ {
 set​.add(​'Tangerine'​);
 } ​catch​(ex) {
  console.log(ex.message); ​//exceeded capacity of 5 elements
 }
 
 set​.​delete​(​'Grape'​);
 set​.add(​'Peach'​);
 console.log(​set​.size); ​//5
 
 const​ set2 = ​new​ BoundedSet(2, [​'Apple'​, ​'Banana'​, ​'Grape'​]);
 console.log(set2.size); ​//0
 console.log(set2); ​//BoundedSet { capacity: 2 }

Exercise 4

 'use strict'​;
 
 class​ Base {
  copy() {
 const​ ​constructor​ =
  Reflect.getPrototypeOf(​this​).​constructor​[Symbol.species] ||
  Reflect.getPrototypeOf(​this​).​constructor​;
 
 return​ ​new​ ​constructor​();
  }
 }
 
 class​ Derived1 ​extends​ Base {
 static​ ​get​ [Symbol.species]() {
 return​ Base;
  }
 }
 
 class​ Derived2 ​extends​ Base {
 static​ ​get​ [Symbol.species]() {
 return​ Derived2;
  }
 }
 
 const​ derived1 = ​new​ Derived1();
 const​ derived2 = ​new​ Derived2();
 
 console.log(derived1.copy()); ​//Base {}
 console.log(derived2.copy()); ​//Derived2 {}

Exercise 5

 'use strict'​;
 
 class​ SpecialWordChecker {
  isSpecial(word) { ​return​ word !== word; }
 }
 class​ PalindromeChecker ​extends​ SpecialWordChecker {
  isSpecial(word) {
 return​ [...word].reverse().join(​''​) === word || ​super​.isSpecial(word);
  }
 }
 
 class​ AlphabeticalChecker ​extends​ SpecialWordChecker {
  isSpecial(word) {
 return​ [...word].sort().join(​''​) === word || ​super​.isSpecial(word);
  }
 }
 
 const​ checkIfSpecial = ​function​(specialWordChecker, word) {
 const​ result = specialWordChecker.isSpecial(word) ? ​'is'​ : ​'is not'​;
  console.log(​`​${word}​ ​${result}​ special`​);
 };
 
 const​ palindromeChecker = ​new​ PalindromeChecker();
 checkIfSpecial(palindromeChecker, ​'mom'​); ​//mom is special
 checkIfSpecial(palindromeChecker, ​'abe'​); ​//abe is not special
 
 const​ alphabeticalChecker = ​new​ AlphabeticalChecker();
 checkIfSpecial(alphabeticalChecker, ​'mom'​); ​//mom is not special
 checkIfSpecial(alphabeticalChecker, ​'abe'​); ​//abe is special
 
 //Combine PalindromeChecker and AlphabeticalChecker here
 const​ alphabeticalAndPalindromeChecker =
  Object.setPrototypeOf(
  Object.getPrototypeOf(​new​ AlphabeticalChecker()),
 new​ PalindromeChecker());
 
 checkIfSpecial(alphabeticalAndPalindromeChecker, ​'mom'​); ​//mom is special
 checkIfSpecial(alphabeticalAndPalindromeChecker, ​'abe'​); ​//abe is special
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset