Day 7: Regular Expressions I Solution
Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 7: Regular Expressions I Solution.
Also visit this link:ย Day 6: JavaScript Dates Solution
Day 7: Regular Expressions I Solution
Objective
In this challenge, we use aย Regular Expressionย to evaluate a string.
Task
Complete the function in the editor below by returning aย RegExpย object,ย re, that matches any stringย sย thatย beginsย andย endsย with the sameย vowel. Recall that the English vowels areย a,ย e,ย i,ย o, andย u.
Constraints
- The length of stringย sย isย =>3ย .
- Stringย sย consists of lowercase letters only (i.e.,ย [a-z]).
Output Format
The function must return aย RegExpย object that matches any stringย sย beginning with and ending in the same vowel.
Sample Input 0
bcd
Sample Output 0
false
Explanation 0
This string starts with (and ends in) a consonant, so it cannot start and end with the same vowel.
Sample Input 1
abcd
Sample Output 1
false
Explanation 1
This string ends in a consonant, so it cannot start and end with the same vowel.
Sample Input 2
abcda
Sample Output 2
true
Explanation 2
This string starts and ends with the same vowel (a).
Sample Input 3
abcdo
Sample Output 3
false
Explanation 3
This string starts with the vowelย aย but ends in the vowelย o.
Solution โ Day 7: Regular Expressions I
'useย strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); letย inputStringย =ย ''; letย currentLineย =ย 0; process.stdin.on('data',ย inputStdinย =>ย { ย ย ย ย inputStringย +=ย inputStdin; }); process.stdin.on('end',ย _ย =>ย { ย ย ย ย inputStringย =ย inputString.trim().split('\n').map(stringย =>ย { ย ย ย ย ย ย ย ย returnย string.trim(); ย ย ย ย }); ย ย ย ย ย ย ย ย main();ย ย ย ย }); functionย readLine()ย { ย ย ย ย returnย inputString[currentLine++]; } functionย regexVar()ย { ย ย ย ย /* ย ย ย ย ย *ย Declareย aย RegExpย objectย variableย namedย 're' ย ย ย ย ย *ย Itย mustย matchย aย stringย thatย startsย andย endsย withย theย sameย vowelย (i.e.,ย {a,ย e,ย i,ย o,ย u}) ย ย ย ย ย */ ย ย ย ย varย reย =ย RegExp(/^([aeiou]).*\1$/); ย ย ย ย /* ย ย ย ย ย *ย Doย notย removeย theย returnย statement ย ย ย ย ย */ ย ย ย ย returnย re; } functionย main()ย { ย ย ย ย constย reย =ย regexVar(); ย ย ย ย constย sย =ย readLine(); ย ย ย ย ย ย ย ย console.log(re.test(s)); }