Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 39d0398

Browse files
Merge pull request #38 from somekindofwallflower/feature/capitalize
Implement the solution to capitalize each word in a given string, wri...
2 parents 80d37e9 + 3bf9b48 commit 39d0398

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

‎08_capitalize/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// --- Directions
2+
// Write a function that accepts a string. The function should
3+
// capitalize the first letter of each word in the string then
4+
// return the capitalized string.
5+
// --- Examples
6+
// capitalize('a short sentence') --> 'A Short Sentence'
7+
// capitalize('a lazy fox') --> 'A Lazy Fox'
8+
// capitalize('look, it is working!') --> 'Look, It Is Working!'
9+
10+
// Solution 1
11+
// function capitalize(str) {
12+
// let result = [];
13+
// for(let word of str.split(" ")) {
14+
// result.push(`${word[0].toUpperCase()}${word.slice(1)}`)
15+
// }
16+
// return result.join(" ")
17+
// }
18+
19+
// Solution 2
20+
// Create a string with the first element of the string capitalized
21+
// For each character of the string if the character on the left is space capitalize and add to the result else add it to the result
22+
function capitalize(str) {
23+
let result = str[0].toUpperCase();
24+
for(let i = 1; i <= str.length; i++) {
25+
if(str[i - 1] === ' ') {
26+
result += str[i].toUpperCase();
27+
} else {
28+
result += str[i]
29+
}
30+
}
31+
return result;
32+
}
33+
34+
module.exports = capitalize;

‎08_capitalize/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const capitalize = require('./index');
2+
3+
test('Capitalize is a function', () => {
4+
expect(typeof capitalize).toEqual('function');
5+
});
6+
7+
test('capitalizes the first letter of every word in a sentence', () => {
8+
expect(capitalize('hi there, how is it going?')).toEqual(
9+
'Hi There, How Is It Going?'
10+
);
11+
});
12+
13+
test('capitalizes the first letter', () => {
14+
expect(capitalize('i love breakfast at bill miller bbq')).toEqual(
15+
'I Love Breakfast At Bill Miller Bbq'
16+
);
17+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /