File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
src/algorithms/capitalize Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ // Option 1
2+ export const capitalize = ( str : string ) => {
3+ const words = [ ] ;
4+
5+ for ( const word of str . split ( ' ' ) ) {
6+ words . push ( word [ 0 ] . toUpperCase ( ) + word . slice ( 1 ) ) ;
7+ }
8+
9+ return words . join ( ' ' ) ;
10+ } ;
11+
12+ // Option 2
13+ export const capitalizeString = ( str : string ) => {
14+ let result = str [ 0 ] . toUpperCase ( ) ;
15+
16+ for ( let i = 1 ; i < str . length ; i ++ ) {
17+ if ( str [ i - 1 ] === ' ' ) {
18+ result += str [ i ] . toUpperCase ( ) ;
19+ } else {
20+ result += str [ i ] ;
21+ }
22+ }
23+
24+ return result ;
25+ } ;
Original file line number Diff line number Diff line change 1+ import { capitalize , capitalizeString } from '../capitalize' ;
2+
3+ describe ( 'Capitalize Algorithm' , ( ) => {
4+ test ( 'Capitalize is a function' , ( ) => {
5+ expect ( typeof capitalize ) . toEqual ( 'function' ) ;
6+ expect ( typeof capitalizeString ) . toEqual ( 'function' ) ;
7+ } ) ;
8+
9+ test ( 'capitalizes the first letter of every word' , ( ) => {
10+ expect ( capitalize ( 'hi there, how is it going?' ) ) . toEqual (
11+ 'Hi There, How Is It Going?' ,
12+ ) ;
13+ expect ( capitalizeString ( 'hi there, how is it going?' ) ) . toEqual (
14+ 'Hi There, How Is It Going?' ,
15+ ) ;
16+ } ) ;
17+ } ) ;
You can’t perform that action at this time.
0 commit comments