Scriptdrill

Level 1: Basics Exercise 7 of 45 Pro

Count the vowels

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Count the vowels a, e, i, o and u in `s`, in upper or lower case. Every other character is ignored, including y and accented letters such as é.

What this exercise teaches

match with the g flag returns an array of all matches, or null when there are none, so the `|| []` fallback keeps length safe. The i flag makes the match case-insensitive.

A hint if you get stuck

A regular expression with the `g` and `i` flags finds every vowel: `s.match(/[aeiou]/gi)`. It returns `null` when there is no match.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Basics