Skip to content

Commit c30b5e4

Browse files
committed
Javascript init
0 parents  commit c30b5e4

File tree

13 files changed

+236
-0
lines changed

13 files changed

+236
-0
lines changed

.eslintrc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"rules": {
3+
"quotes": [2, 'single'],
4+
"semi": 2,
5+
"curly": 2,
6+
"camelcase": 0,
7+
"no-unused-vars": 2,
8+
"no-use-before-define": 2,
9+
"no-underscore-dangle": 2,
10+
"strict": 0,
11+
"no-multi-spaces": 2,
12+
"semi-spacing": 2,
13+
"no-shadow": 2
14+
},
15+
16+
"env": {
17+
"browser": true,
18+
"node": true,
19+
"mocha": true
20+
},
21+
"globals": {
22+
"chrome": false
23+
}
24+
}

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
####Run the code and test
2+
This project uses Mocha and Chai for testing:
3+
4+
<br>
5+
#####Install dependencies:
6+
```bash
7+
npm install
8+
npm install -g mocha
9+
npm install -g chai
10+
```
11+
12+
<br>
13+
#####Run tests:
14+
```bash
15+
mocha --recursive
16+
```
17+
18+
<br>
19+
#####Add new npm modules:
20+
```bash
21+
npm install --save <package name>
22+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = Strings_1_1 = (function() {
2+
return {
3+
/*
4+
* Tests to see if string contains all unique chars.
5+
* @param {String} str - The string to be checked for uniqueness
6+
* @returns {Boolean} true if string has only unique chars. False if a duplicate exists
7+
*/
8+
isUnique: function(str) {
9+
var char_set = Array.apply(null, Array(256)).map(Boolean.prototype.valueOf, false);
10+
for (var i = 0; i < str.length; i++) {
11+
if (char_set[str[i].charCodeAt(0)]) {
12+
return false;
13+
}
14+
char_set[str[i].charCodeAt(0)] = true;
15+
}
16+
return true;
17+
}
18+
};
19+
}());
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = Strings1_2 = (function() {
2+
return {
3+
// Tests to see if two strings are permutations of one another
4+
// Solution #2 from the book. Assumes ascii character set
5+
// @param {String} s - The first string
6+
// @param {String} t - The second string
7+
// @retuns {Boolean} - true if the strings are permutations of one another, false otherwise
8+
isPermutation: function(s, t) {
9+
10+
var sLength = s.length;
11+
var tLength = t.length;
12+
13+
if (sLength !== tLength) {
14+
return false;
15+
}
16+
17+
var s_array = Array.apply(null, Array(256)).map(Number.prototype.valueOf, 0);
18+
19+
for (var i = 0; i < sLength; i++) {
20+
s_array[s[i].charCodeAt(0)]++;
21+
}
22+
23+
for (var i = 0; i < tLength; i++) {
24+
if (--s_array[t[i].charCodeAt(0)] < 0){
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
};
31+
}());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = Strings_1_8 = (function() {
2+
var _isSubstring = function(str1, str2) {
3+
return str1.indexOf(str2) != -1
4+
}
5+
var _sameLengthAndNotBlank = function(str1, str2) {
6+
var len = str1.length;
7+
return len === str2.length && len > 0
8+
}
9+
return {
10+
isRotation: function(str1, str2) {
11+
if(!_sameLengthAndNotBlank(str1, str2)) return false;
12+
if(_isSubstring(str1+str1, str2)) return true;
13+
}
14+
}
15+
}());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = MyQueue = (function(){
2+
3+
function MyQueue() {
4+
if (!(this instanceof MyQueue)) {
5+
return new MyQueue;
6+
}
7+
this.oldStack = [];
8+
this.newStack = [];
9+
};
10+
11+
function shiftStacks() {
12+
if(this.oldStack.length == 0) {
13+
while(this.newStack.length > 0) {
14+
this.oldStack.push(this.newStack.pop());
15+
}
16+
}
17+
};
18+
19+
MyQueue.prototype.push = function(item) {
20+
this.newStack.push(item);
21+
};
22+
23+
MyQueue.prototype.pop = function() {
24+
shiftStacks.call(this);
25+
return this.oldStack.pop();
26+
};
27+
28+
return MyQueue;
29+
})();

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var requireDirectory = require('require-directory');
2+
module.exports = requireDirectory(module);

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "CrackingJS",
3+
"version": "0.0.1",
4+
"description": "Data Structures and Algorithms in Javascript",
5+
"dependencies": {
6+
"mocha": "*",
7+
"chai": "*",
8+
"require-directory": "*",
9+
"sinon": "*"
10+
},
11+
"devDependencies": {
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require('../../test_helper');
2+
describe('1.1 #hasUniqueChars', function () {
3+
var str1, str2;
4+
beforeEach(function () {
5+
str1 = 'abcdef';
6+
str2 = 'qwertyuiopasdfghjkl;\'zxcvbnm,.';
7+
str3 = 'abcdefa';
8+
});
9+
it('returns true if string is all unique chars', function () {
10+
expect(Strings_1_1.isUnique(str1)).to.be.true;
11+
});
12+
it('returns true if string is all unique chars', function () {
13+
expect(Strings_1_1.isUnique(str2)).to.be.true;
14+
});
15+
it('returns false if string contains duplicate chars', function () {
16+
expect(Strings_1_1.isUnique(str3)).to.be.false;
17+
});
18+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require('../../test_helper');
2+
describe('1.2 #isPermutation', function () {
3+
var str1, str2;
4+
beforeEach(function () {
5+
str1 = 'hello';
6+
str2 = 'olleh';
7+
str3 = 'abcdef';
8+
});
9+
it('returns true if strings are permutations', function () {
10+
expect(Strings1_2.isPermutation(str1, str2)).to.be.true;
11+
});
12+
it('returns false if strings are not permutations', function () {
13+
expect(Strings1_2.isPermutation(str1, str3)).to.be.false;
14+
});
15+
});

0 commit comments

Comments
 (0)