DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

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

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)

  1. Makes any vowels capital
  2. Makes any consonants lower case
  3. 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(' ')

}
Enter fullscreen mode Exit fullscreen mode

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

My Github
My twitter
Solve this Kata

Top comments (0)