File tree Expand file tree Collapse file tree 8 files changed +73
-27
lines changed
lib/data-structures/chapter-1
data-structures/chapter-1 Expand file tree Collapse file tree 8 files changed +73
-27
lines changed Original file line number Diff line number Diff line change 1+ node_modules
2+
3+ # Logs
4+ logs
5+ * .log
6+
7+ # Mac specific
8+ DS_Store
Original file line number Diff line number Diff line change 1- ####Run the code and test
2- This project uses Mocha and Chai for testing:
1+ # Cracking the Coding Interview 6th Edition - JavaScript
32
43<br >
5- #####Install dependencies:
4+
5+ ## Install dependencies:
66``` bash
77npm install
8- npm install -g mocha
9- npm install -g chai
108```
119
1210<br >
13- #####Run tests:
11+
12+ ## Test
13+
1414``` bash
15- mocha --recursive
15+ npm test
1616```
1717
18- <br >
19- #####Add new npm modules:
18+ If you just want to lint
2019``` bash
21- npm install --save < package name >
20+ npm run lint
2221```
22+
23+ <br >
Original file line number Diff line number Diff line change 1+ module . exports = Strings_1_3 = ( function ( ) {
2+ return {
3+ // Generate URL freindly strings of a specified length
4+ // Solution #3 from the book.
5+ // @param {String } s - Standard string with potential trailing whitespace
6+ // @param {Number } l - The "true" length of the string
7+ // @retuns {String} - A URL freindly string of the specified length
8+ URLify : function ( s , l ) {
9+ var newString = '' ;
10+ for ( var i = 0 ; i < l ; i ++ ) {
11+ if ( s . charAt ( i ) === ' ' ) {
12+ newString += '%20' ;
13+ } else {
14+ newString += s . charAt ( i ) ;
15+ }
16+ }
17+ return newString ;
18+ }
19+ } ;
20+ } ( ) ) ;
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ module.exports = Strings1_4 = (function() {
66 // @retuns {Boolean} - true if a permutation of palindrome exists, false if it does not
77 palindromePermutation : function ( str ) {
88 var distinct = 0 ;
9- var s_array = Array . apply ( null , Array ( 256 ) ) . map ( Number . prototype . valueOf , 0 ) ;
9+ var s_array = Array . apply ( null , Array ( 256 ) ) . map ( Number . prototype . valueOf , 0 ) ;
1010 str = str . toLowerCase ( ) ;
1111 for ( var i = 0 ; i < str . length ; i ++ ) {
1212 if ( str [ i ] == ' ' ) {
@@ -21,5 +21,5 @@ module.exports = Strings1_4 = (function() {
2121 }
2222 return ( distinct < 2 ) ;
2323 }
24- }
24+ } ;
2525} ( ) ) ;
Original file line number Diff line number Diff line change 11module . exports = Strings_1_8 = ( function ( ) {
2- var _isSubstring = function ( str1 , str2 ) {
3- return str1 . indexOf ( str2 ) != - 1
4- }
5- var _sameLengthAndNotBlank = function ( str1 , str2 ) {
2+ var isSubstring = function ( str1 , str2 ) {
3+ return str1 . indexOf ( str2 ) != - 1 ;
4+ } ;
5+
6+ var sameLengthAndNotBlank = function ( str1 , str2 ) {
67 var len = str1 . length ;
7- return len === str2 . length && len > 0
8- }
8+ return len === str2 . length && len > 0 ;
9+ } ;
10+
911 return {
1012 isRotation : function ( str1 , str2 ) {
11- if ( ! _sameLengthAndNotBlank ( str1 , str2 ) ) return false ;
12- if ( _isSubstring ( str1 + str1 , str2 ) ) return true ;
13+ if ( ! sameLengthAndNotBlank ( str1 , str2 ) ) {
14+ return false ;
15+ }
16+ if ( isSubstring ( str1 + str1 , str2 ) ) {
17+ return true ;
18+ }
1319 }
14- }
20+ } ;
1521} ( ) ) ;
Original file line number Diff line number Diff line change 11{
22 "name" : " CrackingJS" ,
3- "version" : " 0.0.1 " ,
3+ "version" : " 0.0.2 " ,
44 "description" : " Data Structures and Algorithms in Javascript" ,
5+ "scripts" : {
6+ "lint" : " eslint lib/**/*.js test/**/*.js" ,
7+ "test" : " npm run lint && mocha --recursive"
8+ },
59 "dependencies" : {
6- "mocha" : " *" ,
710 "chai" : " *" ,
11+ "eslint" : " ^2.2.0" ,
12+ "mocha" : " *" ,
813 "require-directory" : " *" ,
914 "sinon" : " *"
1015 },
11- "devDependencies" : {
12- }
16+ "devDependencies" : {}
1317}
Original file line number Diff line number Diff line change 1+ require ( '../../test_helper' ) ;
2+ describe ( '1.3 #URLify' , function ( ) {
3+ it ( 'returns a URL freindly string of the specified length' , function ( ) {
4+ expect ( Strings_1_3 . URLify ( 'much ado about nothing ' , 22 ) ) . to . be . equal ( 'much%20ado%20about%20nothing' ) ;
5+ expect ( Strings_1_3 . URLify ( 'Mr John Smith ' , 13 ) ) . to . be . equal ( 'Mr%20John%20Smith' ) ;
6+ } ) ;
7+ } ) ;
Original file line number Diff line number Diff line change 11require ( '../lib' ) ;
2- module . exports = sinon = require ( " sinon" ) ;
3- module . exports = expect = require ( " chai" ) . expect
2+ module . exports = sinon = require ( ' sinon' ) ;
3+ module . exports = expect = require ( ' chai' ) . expect ;
You can’t perform that action at this time.
0 commit comments