File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
lib/data-structures/chapter-1
test/data-structures/chapter-1 Expand file tree Collapse file tree 2 files changed +27
-0
lines changed 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 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+ } ) ;
You can’t perform that action at this time.
0 commit comments