Created
July 17, 2017 16:46
-
-
Save junjunparkpark/950cdc9d5f3a27e0dfc17108ea6e22dc to your computer and use it in GitHub Desktop.
vowel-doubler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inputs: Array of 1 character strings | |
// Outputs: The same array of character strings with all the vowels 'doubled' | |
// Constraints: Do not manipulate strings nor create data structures or a new array. | |
// Edge cases: None | |
// Pseudocode implementation: | |
// Create an object that one can compare each character in the array to, indicating whether a char is a vowel. | |
// For each index in the array, starting at the 0 index up to arr.length - 1 | |
// Create a current character variable that will represent the string at a given index in the array | |
// If the char is a vowel | |
// Splice the array at the current index | |
// Add the current character | |
// Increment the index by 1 so that one will not run into the same character again on the next iteration of this loop | |
// Return the original array | |
const vowelDoubler = (arr) => { | |
const vowels = {'a': true, 'e': true, 'i': true, 'o': true, 'u': true}; | |
for (let i = 0; i < arr.length; i++) { | |
let char = arr[i]; | |
if (vowels[char]) { | |
arr.splice(i, 0, char); | |
i++; | |
} | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment