Chapter 4

Here are the solutions to the Exercises, for the Chapter 4, Iterators and Symbols chapter.

Exercise 1

 const​ letters = [​'a'​, ​'b'​, ​'c'​, ​'d'​, ​'e'​, ​'f'​, ​'g'​, ​'h'​];
 
 for​(​const​ [i, letter] ​of​ letters.entries()) {
 if​(i % 3 === 0)
  console.log(letter);
 }

Exercise 2

 const​ numbers = [1, 2, 3];
 
 console.log(​"The Symbol properties in arrays are:"​);
 
 console.log(
  Object.getOwnPropertySymbols(Object.getPrototypeOf(numbers)));

Exercise 3

 class​ Message {
 constructor​(text) { ​this​.text = text; }
 
  [Symbol.replace](word, substitute) {
 return​ ​this​.text.replace(word, substitute);
  }
 }
 
 const​ message = ​new​ Message(​'There are no stupid questions.'​);
 
 console.log(​'stupid'​.replace(message, ​'s*****'​));
 //There are no s***** questions.
 
 console.log(​''​.replace(message, ​'Yes, '​));
 //Yes, There are no stupid questions.

Exercise 4

 const​ fibonocciSeries = ​function​*() {
 let​ current = 1;
 let​ next = 1;
 
 yield​* [current, next];
 
 while​(​true​) {
 const​ temp = current;
  current = next;
  next = next + temp;
 yield​ next;
  }
 }
 
 for​(​const​ value ​of​ fibonocciSeries()) {
 if​(value > 25) ​break​;
  process.stdout.write(value + ​", "​);
 }

Exercise 5

 const​ fibonocciSeries = ​function​*() {
 let​ current = 1;
 let​ next = 1;
 
 let​ index = 0;
 
 yield​ *[[index++, current], [index++, next]];
 
 while​(​true​) {
 const​ temp = current;
  current = next;
  next = next + temp;
 
 yield​ [index++, next];
  }
 }
 
 for​(​const​ [index, value] ​of​ fibonocciSeries()) {
 if​(index > 8) ​break​;
  process.stdout.write(value + ​", "​);
 }
..................Content has been hidden....................

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