Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callenge Solved With Deque #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/challenge.js
Original file line number Diff line number Diff line change
@@ -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()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usa nombres de variables más descriptivas, recuerda que js usa el patrón camel case

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usa const

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 };
95 changes: 95 additions & 0 deletions src/deque.js
Original file line number Diff line number Diff line change
@@ -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 };