Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Checking for vowels and consonants in a string and printing them out

I'm doing a challenge on Hackerrank, the task is to check for vowels and consonants in a string and printing both (ordered) in separate lines. I can print out the vowels just fine but can't do the same with consonants. It won't output them. Here's my code:

    const vowels = 'aeiou'
    
    var consonants = []
    for (var i=0;i<s.length;i++) {
        if(vowels.includes(s[i])) {console.log(s[i])
        } 
        else {
            consonants+= s[i] + '\n'
        }
     
    } 
    
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can simply achieve that by using regex and string.match() method.

Live Demo :

const str = "Separate the vowels and consonants from a string"; 
const vowels = str.match(/[aeiou]/gi); 
const consonants = str.match(/[^aeiou]/gi); 

console.log(vowels);
console.log(consonants);

about 4 years ago · Juan Pablo Isaza Report

0

You can use regex and string.match() to split the vowels and consonants. like this

const s = "hello World";
const [vowels, consonants] = [s.match(/[aiueo]/gi), s.match(/[^aiueo\s]/gi)];
vowels.forEach(o => console.log("vowels => "+o))
consonants.forEach(o => console.log("consonants => "+o))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!