I was doing Hacker Rank Javascript 10 Day Challenge in which i saw a problem and thought that i would share.
Q: Complete the vowelsAndConsonants function in the editor below. It has one parameter, a string, , consisting of lowercase English alphabetic letters (i.e., a through z). The function must do the following:
First, print each vowel in on a new line. The English vowels are a, e, i, o, and u, and each vowel must be printed in the same order as it appeared in .
Second, print each consonant (i.e., non-vowel) in on a new line in the same order as it appeared in .
I have done this by adding two loops in my function
function vowelsAndConsonants(s) {
let vowel = ['a','e','i','o','u']
for(var i=0; i< s.length;i++){
if(vowel.includes(s.charAt(i))){
console.log(s.charAt(i))
}
}
for(var i=0; i< s.length;i++){
if(!vowel.includes(s.charAt(i))){
console.log(s.charAt(i))
}
}
}
Calling the function
vowelsAndConsonants(javascript)
Output:
a
a
i
o
o
j
v
s
c
r
p
t
l
p
s
Observe:
Each letter is printed on a new line.
Then the vowels are printed in the same order as they appeared in the input
Then the consonants are printed in the same order as they appeared in input
Hope you find this helpful and if there is any optimize method to do this kindly tell me in the comment section
Happy Coding!
Top comments (0)