Skip to content

Commit 9fc9c89

Browse files
committed
added answer for ch1_q3
1 parent e82f05c commit 9fc9c89

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}());
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
});

0 commit comments

Comments
 (0)