diff --git a/src/challenge.js b/src/challenge.js index f91816b..2cab6c5 100644 --- a/src/challenge.js +++ b/src/challenge.js @@ -1,10 +1,24 @@ 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 (let i = 0; i < text.length; i++) Deq.addBack(text.charAt(i)) + + for(let 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