Skip to Content
Learning JavaScript Data Structures and Algorithms - Third Edition
book

Learning JavaScript Data Structures and Algorithms - Third Edition

by Loiane Avancini
April 2018
Beginner to intermediate content levelBeginner to intermediate
426 pages
10h 19m
English
Packt Publishing
Content preview from Learning JavaScript Data Structures and Algorithms - Third Edition

Creating the toString method

And we are done! Our Queue class is implemented. Just like we did for the Stack class, we can also add the toString method:

toString() {  if (this.isEmpty()) {    return '';  }  let objString = `${this.items[this.lowestCount]}`;  for (let i = this.lowestCount + 1; i < this.count; i++) {    objString = `${objString},${this.items[i]}`;  }  return objString;} 

In the Stack class, we started to iterate the items values from index zero. As the first index of the Queue class might not be zero, we start iterating it from the lowestCount index.

And now we are really done!

The Queue and Stack classes are very similar. The main difference comes with the dequeue and peek methods, which is because of the difference between the FIFO and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning JavaScript Data Structures and Algorithms

Learning JavaScript Data Structures and Algorithms

Loiane Avancini

Publisher Resources

ISBN: 9781788623872Supplemental Content