DEV Community

Thaísa Vieira
Thaísa Vieira

Posted on

HackerRank Challenges

I'm trying other websites to exercises DSA and I started with HackerRank, 10 days of JavaScript. In this journey my focus is to spend the necessary time to think, search and solve the problems, without being so fixed with deliver challenges in the right days.

Also, I'd like some help with a basic problem with loop:
10 Days of JavaScript - Day 2 (In the link you can check the problem and the see the complete code).
The main problem is to complete the vowelsAndConsonants function that print each vowel in on a new line in the same order as it appeared and after do the same thing for consonants too.

What I did was start with an array storing the vowels and separe in two parts, vowels and not vowels.
In the for loop I started a counter that started at zero, was smaller than the size of the string that the user would provide and the step would be i = i + 1. Then I ask console.log to display the string entered by the user within the function conditions according to the for loop.

function vowelsAndConsonants(s) {
    let vowels = ['a', 'e', 'i', 'o','u'];

    for(let i=0; i<s.lenght; i++){
    if(vowels.includes(s[i]))
    console.log(s[i]);
    }


    for(let i=0;i<s.lenght;i++){
    if(!vowels.includes(s[i]))
    console.log(s[i]); 
    }

}
Enter fullscreen mode Exit fullscreen mode

input:

javascriptloops

Enter fullscreen mode Exit fullscreen mode

my output:

~ no response on stdout ~

Enter fullscreen mode Exit fullscreen mode

expected output:

a
a
i
o
o
j
v
s
c
r
p
t
l
p
s
Enter fullscreen mode Exit fullscreen mode

In the exercise itself there is a main function that calls the function vowelsAndConsonants and captures what the user types.
But I'm not understanding what I'm doing wrong for my output don't return anything. Someone can help me?

Top comments (10)

Collapse
 
rivea0 profile image
Eda

Hey Thaísa, your implementation is actually correct, but there are typos in the for loops: s.lenght should be s.length, so they might be the culprits.

With the same input you provided (javascriptloops) I'm seeing the expected output when the typos are fixed.

Collapse
 
thaisavieira profile image
Thaísa Vieira

Hello, Eda! Thanks for being kind to me, typos errors are being frequent. I'll pay more attention and also keep doing those exercises to practice and familiarize myself with them!

Collapse
 
rivea0 profile image
Eda

Absolutely, no problem! It's usually the little things that trip us up 💁🏻‍♀️

Thread Thread
 
thaisavieira profile image
Thaísa Vieira

Thanks for the kindness!

Collapse
 
francis_ngugi profile image
Francis Ngugi

If it were me I would have done the output of the vowels then exclude them from the string meaning a new string will be formed without the vowels in the new string, and then print out the new string that was formed.

Collapse
 
francis_ngugi profile image
Francis Ngugi

Now I see the error. This qsn aims to compare each letter in the string to see if it is a vowel... So to do that when U r iterating the string you will have to store the letters in a variable one by one during incrementing of the loop to compare the vowels with the letter stored in the variable.

Collapse
 
francis_ngugi profile image
Francis Ngugi

And I don't know if I am wrong but in cases where I do developing in React when using includes and use it with the filter function or u can just do the qsn by manually comparing. But using the filter function might help u in this qsn

Collapse
 
francis_ngugi profile image
Francis Ngugi

Also, ensure you have a proper indentation of your code.

Collapse
 
francis_ngugi profile image
Francis Ngugi

Doing challenges with JS aint easy

Collapse
 
thaisavieira profile image
Thaísa Vieira

Hello, Francis! Thanks for your tips, they're of great help.