Skip to content

Commit 98922bf

Browse files
committed
add
1 parent 953af21 commit 98922bf

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

2023-08-17-uS.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// https://www.codewars.com/kata/64d9f8b2dd14d200247b8184/train/javascript
2+
// FIXME: kinda works... but won't pass the test "should handle lots of names" neither "Should work for random names"
3+
// * the indexOf logic is not correct.
4+
function generateChatRoomNames( users ) {
5+
users = users
6+
.sort( ( a, b ) => a.localeCompare( b ) )
7+
.map( ( user ) => user
8+
.split( ' ' )
9+
.map( ( name ) => name[0].toUpperCase() + name.slice( 1 ).toLowerCase() )
10+
.join( ' ' ),
11+
);
12+
13+
const firstNames = users.map( ( user ) => user.split( ' ' )[0] );
14+
const lastNameInitials = users.map( ( user ) => user.split( ' ' )[1][0] );
15+
16+
return users
17+
.map( ( user, i ) => {
18+
const [ firstName, lastName ] = user.split( ' ' );
19+
20+
const isEqualFirstName = firstNames.indexOf( firstName ) !== firstNames.lastIndexOf( firstName );
21+
const isEqualLastNameIntital = lastNameInitials[firstNames.indexOf( firstName )] === lastNameInitials[firstNames.lastIndexOf( firstName )];
22+
23+
if ( isEqualFirstName && isEqualLastNameIntital ) {
24+
return `${firstName} ${lastName}`;
25+
} else if ( isEqualFirstName ) {
26+
return `${firstName} ${lastName[0]}`;
27+
} else {
28+
return firstName;
29+
}
30+
} );
31+
}
32+
33+
console.log( generateChatRoomNames( [
34+
'JOE Bloggs',
35+
'John Smith',
36+
] ) ); // :>> ["Joe", "John"]
37+
console.log( generateChatRoomNames( [
38+
'Joe Bloggs',
39+
'John Smith',
40+
'Jane Doe',
41+
] ) ); // :>> ["Jane", "Joe", "John"]
42+
console.log( generateChatRoomNames( [
43+
'Joe Bloggs',
44+
'John Smith',
45+
'Jane Doe',
46+
'Jane Bloggs',
47+
] ) ); // :>> ["Jane B", "Jane D", "Joe", "John"]
48+
console.log( generateChatRoomNames( [
49+
'Joe Bloggs',
50+
'John Smith',
51+
'Jane Doe',
52+
'Jane Bloggs',
53+
'John Scott',
54+
] ) ); // :>> ["Jane B", "Jane D", "Joe", "John Scott", "John Smith"]
55+
// FIXME: see.. there are 2x Jane with B... means, it should display the fullnames.
56+
console.log( generateChatRoomNames( [
57+
'Joe Bloggs',
58+
'Jane Smith',
59+
'John Smith',
60+
'Jane Doe',
61+
'Jane Bright',
62+
'Jane Bloggs',
63+
'John Scott',
64+
] ) ); // :>> ["Jane B", "Jane D", "Joe", "John Scott", "John Smith"]

0 commit comments

Comments
 (0)