-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMock Interview - reverseVowelsInString.js
More file actions
58 lines (43 loc) · 1.32 KB
/
Mock Interview - reverseVowelsInString.js
File metadata and controls
58 lines (43 loc) · 1.32 KB
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
function reverseVowelsInString (str) {
function isVowel (s) {
return s == "A" || s == "E" || s == "I" || s == "O" || s == "U" || s == "a" || s == "e" || s == "i"|| s == "o"|| s == "u";
}
var vowels = [];
var indexes = [];
var newStr = str.split('');
for (let i = 0; i < str.length; i++) {
if (isVowel(str[i])) {
vowels.push(str[i]);
indexes.push(i);
}
}
vowels.reverse();
for (let i = 0; i < vowels.length; i++){
newStr[indexes[i]] = vowels[i];
}
return newStr.join('');
}
reverseVowelsInString('Acknowledgement');
/*
I was looking at a course that was having a preview for a mock interview.
The interviewer asked the interviewee to reverse all of the vowels in a string.
I wanted to work through a solution prior watching the complete mock interview.
This also works as a good practive for expanding my knowledge.
*/
// refactored solution:
function reverseVowels(str) {
str = str.split('');
let i = 0;
let j = str.length - 1;
do {
if ( (/[aeiou]/i.test(str[i]) ) && ( /[aeiou]/i.test(str[j]) ) ) {
[ str[i] , str[j] ] = [ str[j] , str[i] ];
i++;
j--;
}
else if (/[aeiou]/i.test(str[i])) j--;
else i++;
} while (i < j);
return str.join('');
}
reverseVowels('Acknowledgement');