-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-08-08.js
51 lines (43 loc) · 1.59 KB
/
2023-08-08.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// https://www.codewars.com/kata/570957fc20a35bd2df0004f9/train/javascript
function AlanAnnoyingKid( input ) {
const str = input.replace( /Today I /, '' );
// identify if Alan "did" or "didn't"
const alanDid = !/didn't/.test( str );
// get verb, based on if Alan "did" or "didn't"
let verb = '';
if ( alanDid ) {
verb = str.match( /.+(?<=ed)/ )[0];
} else {
verb = str.match( /didn't \w+/ )[0];
}
// get object
const regEx = new RegExp( `(${verb} )|(.)$`, 'g' );
const object = str.replace( regEx, '' );
// create output string, based on if Alan "did" or "didn't"
let output = '';
if ( alanDid ) {
output = `didn't ${verb.replace( /ed$/, '' )} at all!`;
} else {
output = `did ${verb.replace( /didn't /, '' )} it!`;
}
return `I don't think you ${verb} ${object} today, I think you ${output}`;
}
console.log(
AlanAnnoyingKid( 'Today I played football.' ) );
console.log(
AlanAnnoyingKid( 'Today I didn\'t play football.' ) );
console.log(
AlanAnnoyingKid( 'Today I didn\'t attempt to hardcode this Kata.' ) );
// others solution
function AlanAnnoyingKid2( input ) {
const action = input.slice( 8, -1 );
const isNegation = input.indexOf( 'didn\'t' ) > 0;
const verb = isNegation ? action.split( ' ' )[1] : action.match( /\w+(?=ed)/ )[0];
return `I don't think you ${action} today, I think you ${isNegation ? 'did ' + verb + ' it!' : 'didn\'t ' + verb + ' at all!' }`;
}
console.log(
AlanAnnoyingKid2( 'Today I played football.' ) );
console.log(
AlanAnnoyingKid2( 'Today I didn\'t play football.' ) );
console.log(
AlanAnnoyingKid2( 'Today I didn\'t attempt to hardcode this Kata.' ) );