Instructions
Create a function that takes a string as a parameter and does the following, in this order:
Replaces every letter with the letter following it in the alphabet (see note below)
- Makes any vowels capital
- Makes any consonants lower case
- Note: the alphabet should wrap around, so Z becomes A
Example:
Input --> "Cat30"
Output --> "dbU30"
Process --> (Cat30 --> Dbu30 --> dbU30)
My solution:
function changer(s) {
s= s.toLowerCase()
return s.split(' ').map(word=>
word.split('').map(letter=>{
if(letter === 'z' ) return 'A'
if(letter === '0') return '0'
let x = parseInt(letter) ? letter : String.fromCharCode(letter.charCodeAt(letter.length - 1) + 1)
if(/([aeiou])/g.test(x)) return x.toUpperCase()
return x
}).join('')
).join(' ')
}
Explanation
First I changed all the string to lower case.
s= s.toLowerCase()
After that I splitted the string between every space, this for the ones that are strings with more than 2 words
Example:
1-
Input--> 'foo'
Output --> ['foo']
2-
Input --> 'Hello World'
Output ['Hello', 'World']
Then I mapped this array, and I splitted each word of the array
Input --> ['Hello', 'World']
Output --> [['H', 'e', 'l', 'l', 'o'], ['W','o','r','l','d']]
After this I used a conditional that checked if the letter is 'z' it would return 'A' if it is '0' it would return '0'
if(letter === 'z' ) return 'A'
if(letter === '0') return '0'
Then I did the variable x that checked if you can parseInt(letter) it will return letter, because that means it is a number, if not, it will change the letter for the next one in the vocabulary.
let x = parseInt(letter) ? letter : String.fromCharCode(letter.charCodeAt(letter.length - 1) + 1)
'Cat30'
['d','b','u','3','0']
After that, I used a conditional that checked with a regular expression if the x variable (that represents the next letter in the vocabulary of the original letter), is a vowel, if it is a vowel it'll .upperCase() it
['d','b','u','3','0']
['d','b','U','3','0']
At the end I just joined the word array
[['I', 'f', 'm', 'm', 'p'], ['x', 'p', 's', 'm', 'E']]
['Ifmmp', 'xpsmE']
And I joined and returned the last array for the strings that have spaces between them
['Ifmmp', 'xpsmE']
'Ifmmp xpsmE'
What do you think about this solution? ππ€
Top comments (0)