From de7e9947562f236fcf841172756e173cd8941b56 Mon Sep 17 00:00:00 2001 From: Razielini Date: Wed, 6 May 2020 21:44:02 -0600 Subject: [PATCH 1/2] Callenge Solved With Deque I dont know, but my github cant push new mods. --- src/challenge.js | 23 +++++++++++- src/deque.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/deque.js diff --git a/src/challenge.js b/src/challenge.js index f91816b..668064b 100644 --- a/src/challenge.js +++ b/src/challenge.js @@ -1,10 +1,29 @@ const { Deque } = require ('./deque.js'); function palindromeChecker(text) { + if (!text) { + return false + } - // your code + const re = /[^A-Za-z0-9]/g; + text = text.toLowerCase().replace(re, '') - return; + let Deq = new Deque() + for (var i = 0; i < text.length; i++) + { + Deq.addFront(text.charAt(i)) + } + + for(var i = 1; i <= Deq.size(); i++) { + if (Deq.peekBack() !== Deq.peekFront()) { + return false + } else { + Deq.removeBack() + Deq.removeFront() + } + } + + return true; } module.exports = { palindromeChecker }; \ No newline at end of file diff --git a/src/deque.js b/src/deque.js new file mode 100644 index 0000000..7bcd585 --- /dev/null +++ b/src/deque.js @@ -0,0 +1,95 @@ +class Deque { + + constructor() { + this.count = 0; + this.topCount = 0; + this.items = {}; + } + + addFront(element) { + if (this.isEmpty()) { + this.addBack(element); + } else if (this.topCount > 0) { + this.topCount--; + this.items[this.topCount] = element; + } else { + for (let i = this.count; i > 0; i--) { + this.items[i] = this.items[i - 1]; + } + this.count++; + this.topCount = 0; + this.items[0] = element; + } + } + + addBack(element) { + this.items[this.count] = element; + this.count++; + } + + removeFront() { + if (this.isEmpty()) { + return undefined; + } + const rta = this.items[this.topCount]; + delete this.items[this.topCount]; + this.topCount++; + return rta; + } + + removeBack() { + if (this.isEmpty()) { + return undefined; + } + this.count--; + const result = this.items[this.count]; + delete this.items[this.count]; + return result; + } + + size() { + return this.count - this.topCount; + } + + isEmpty() { + return this.size() === 0; + } + + getItems() { + return this.items; + } + + peekFront() { + if (this.isEmpty()) { + return undefined; + } + return this.items[this.topCount]; + } + + peekBack() { + if (this.isEmpty()) { + return undefined; + } + return this.items[this.count - 1]; + } + + toString() { + if (this.isEmpty()) { + return ''; + } + let result = ''; + for (let index = this.topCount; index < this.count; index++) { + result+= `${this.items[index]},`; + } + return result; + } + + clear() { + this.count = 0; + this.topCount = 0; + this.items = {}; + } + +} + +module.exports = { Deque }; \ No newline at end of file From b67e2d96940ee6c078cb55bbc36abc9eeddc328a Mon Sep 17 00:00:00 2001 From: Razielini Date: Sat, 9 May 2020 17:40:40 -0600 Subject: [PATCH 2/2] Update challenge.js --- src/challenge.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/challenge.js b/src/challenge.js index 668064b..2cab6c5 100644 --- a/src/challenge.js +++ b/src/challenge.js @@ -1,20 +1,15 @@ const { Deque } = require ('./deque.js'); function palindromeChecker(text) { - if (!text) { - return false - } + if (!text) return false const re = /[^A-Za-z0-9]/g; text = text.toLowerCase().replace(re, '') let Deq = new Deque() - for (var i = 0; i < text.length; i++) - { - Deq.addFront(text.charAt(i)) - } + for (let i = 0; i < text.length; i++) Deq.addBack(text.charAt(i)) - for(var i = 1; i <= Deq.size(); i++) { + for(let i = 1; i <= Deq.size(); i++) { if (Deq.peekBack() !== Deq.peekFront()) { return false } else {