Skip to content

Commit

Permalink
added new problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarlsson committed Oct 22, 2018
1 parent 126f7dd commit a3c2f90
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ const emitor = new Emitter();
const callback1 = function(x, y) {console.log(x + y)}
const callback2 = function(x, y) {console.log(y + x)}

// emitor.addListener('event_1', callback1);
// emitor.addListener('event_1', callback2);
emitor.addListener('event_1', callback1);
emitor.addListener('event_1', callback2);
//
// console.log('emitor', emitor);
//
Expand Down
25 changes: 25 additions & 0 deletions src/chainableArthmatic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const init = num => {
return new ChainableArtimatic(num);
};

class ChainableArtimatic {
constructor(num) {
this._value = num;
}

value() {
return this._value;
}

add(num) {
this._value = this._value + num;
return this;
}
}

console.log(
init(3)
.add(4)
.add(2)
.value()
);
16 changes: 16 additions & 0 deletions src/flattenArr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// input [1, 3, [4,6], 0, [ [3, 9], 88]]
// output [1, 3, 4, 6, 0, 3, 9, 88]

const flattenArr = (arr, flatArr = []) => {
arr.forEach(el => {
if (Array.isArray(el)) {
return flattenArr(el, flatArr);
}
flatArr.push(el);
});

return flatArr;
};

const test1 = [1, 3, [4, 6], 0, [[3, 9], 88]];
console.log(flattenArr(test1));
25 changes: 25 additions & 0 deletions src/isPalindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// O(2N) => O(N)
const isPalindrome = str => {
let isValidPalindrome = true;
let stack = [];

Array.from(str).forEach(char => {
stack.push(char);
});

Array.from(str).forEach(char => {
const poppedChar = stack.pop();

if (char !== poppedChar) {
isValidPalindrome = false;
}
});

return isValidPalindrome;
};

const testStrings = ["abba", "tacocat", "hannah", "cat", "dog"];

testStrings.forEach(str => {
console.log(isPalindrome(str));
});
1 change: 1 addition & 0 deletions src/playground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('[]==0', []===0);

0 comments on commit a3c2f90

Please sign in to comment.