DEV Community

Jeremy
Jeremy

Posted on • Edited on

3

Journey to Become a Better Programmer (Day: 1)

Today I worked on HackerRank and completed day 0-1 and did some other ones for practice."
I feel really good about this problem, came up with a solution that I am very happy about.

The goal:

  1. 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.

  2. Second, print each consonant (i.e., non-vowel) in on a new line in the same order as it appeared in.

example:
"devto"

should print like so:
e
o
d
v
t

The first thing I did was create an array let strArr=s.split('') out of the string using .split('') ( @laurieontech had a good topic on the called Splice! Slice! Shoot, I meant Shift!) then a few more, one for the vowel let v = [] and one for the consonants let c = [].

next, I created a static array of just vowels

let vowels = ['a','e','i','o','u'].

then, I did a forEach() loop (goes through every item in the array) (set up like this: strArr.forEach(letter=>{}) 'letter' representing each item), in the loop I checked if the vowels (vowels) array includes the letter that we are on in the loop if(vowels.includes(letter)) if that was true I would push the 'letter' to a new vowel array (v) and the consonants go into their own

array (c). Lastly return it all in one array return [...v ...c].join('\n') then .join('\n') them all to one string creating a new line at every letter.

With it put together it looked like this:

function vowelsAndConsonants(s) {
  let strArr = s.split('');
  let vowels = ['a', 'e', 'i', 'o', 'u'];
  let v = [];
  let c =[];

  strArr.forEach(letter => {
    if (vowels.includes(letter)) {
      v.push(letter);
    } else {
      c.push(letter);
    }
  })

  return [...v, ...c].join('\n');
}

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay