DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

#23 - Change it up CodeWars Kata (6 kyu)


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)

  1. Makes any vowels capital
  2. Makes any consonants lower case
  3. Note: the alphabet should wrap around, so Z becomes A


Input --> "Cat30"
Output --> "dbU30"
Process --> (Cat30 --> Dbu30 --> dbU30)

My solution:

function changer(s) { 
  s= s.toLowerCase()

  return s.split(' ').map(word=>

      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(' ')

Enter fullscreen mode Exit fullscreen mode


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


Input--> 'foo'
Output --> ['foo']


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)


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


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? 👇🤔

My Github
My twitter
Solve this Kata

Discussion (0)