HackerRank Challenges
Thaísa Vieira

Thaísa Vieira @thaisavieira

About: 25y, IT newbie student and cat lover.

Location:
Brazil
Joined:
Nov 10, 2023

HackerRank Challenges

Publish Date: Jun 22 '24
8 10

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?

Comments 10 total

  • Francis Ngugi
    Francis NgugiJun 22, 2024

    Doing challenges with JS aint easy

    • Thaísa Vieira
      Thaísa VieiraJun 22, 2024

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

  • Francis Ngugi
    Francis NgugiJun 22, 2024

    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.

  • Francis Ngugi
    Francis NgugiJun 22, 2024

    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

  • Francis Ngugi
    Francis NgugiJun 22, 2024

    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.

  • Francis Ngugi
    Francis NgugiJun 22, 2024

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

  • Eda
    EdaJun 22, 2024

    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.

    • Thaísa Vieira
      Thaísa VieiraJun 22, 2024

      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!

      • Eda
        EdaJun 22, 2024

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

Add comment