From 45b39df267bd775f033d51aab437002baa06e6f0 Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Sat, 15 May 2021 01:47:07 +0300 Subject: [PATCH 1/2] add iterator --- iterator/index.html | 25 +++++++++++++++++++++++++ iterator/iterator.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 iterator/index.html create mode 100644 iterator/iterator.js diff --git a/iterator/index.html b/iterator/index.html new file mode 100644 index 0000000..c9b1234 --- /dev/null +++ b/iterator/index.html @@ -0,0 +1,25 @@ + + + + + Title + + + + + + \ No newline at end of file diff --git a/iterator/iterator.js b/iterator/iterator.js new file mode 100644 index 0000000..163f29c --- /dev/null +++ b/iterator/iterator.js @@ -0,0 +1,41 @@ +// Iterator +class Iterator { + constructor(data) { + this.data = data; + this.index = 0; + } + + //get the first item from the collection + getFirst() { + return this.data.get(this.index); + } + + //get next item from the collection + getNext() { + if (this.hasNext()) { + this.index++; + return this.data.get(this.index); + } + } + + //check if there is a next element + hasNext() { + return this.data.has(this.index + 1) + } + + //get the elements whose values are numbers + getNumbers() { + for (let [key, value] of this.data.entries()) { + if (!Number.isInteger(value)) { + this.data.delete(key); + } + } + return this.data; + } + + //returns a pointer to the first element + reset() { + this.index = 0; + } + +} From 13858c3c63550dee76708c309c8ac734aeb00e05 Mon Sep 17 00:00:00 2001 From: ShGulnaz Date: Thu, 20 May 2021 17:08:21 +0300 Subject: [PATCH 2/2] some fix --- iterator/index.html | 25 +++++++++++++++++-------- iterator/iterator.js | 34 ++-------------------------------- 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/iterator/index.html b/iterator/index.html index c9b1234..2ba7368 100644 --- a/iterator/index.html +++ b/iterator/index.html @@ -11,15 +11,24 @@ \ No newline at end of file diff --git a/iterator/iterator.js b/iterator/iterator.js index 163f29c..58beafb 100644 --- a/iterator/iterator.js +++ b/iterator/iterator.js @@ -1,41 +1,11 @@ // Iterator class Iterator { constructor(data) { - this.data = data; - this.index = 0; - } - - //get the first item from the collection - getFirst() { - return this.data.get(this.index); + this.data = data.values(); } //get next item from the collection getNext() { - if (this.hasNext()) { - this.index++; - return this.data.get(this.index); - } + return this.data.next().value; } - - //check if there is a next element - hasNext() { - return this.data.has(this.index + 1) - } - - //get the elements whose values are numbers - getNumbers() { - for (let [key, value] of this.data.entries()) { - if (!Number.isInteger(value)) { - this.data.delete(key); - } - } - return this.data; - } - - //returns a pointer to the first element - reset() { - this.index = 0; - } - }