mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-16 07:00:53 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1.7 KiB
1.7 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6814d8e1516e86b171929de4 | JavaScript Challenge 1: Vowel Balance | 28 | javascript-challenge-1 |
--description--
Given a string, determine whether the number of vowels in the first half of the string is equal to the number of vowels in the second half.
- The string can contain any characters.
- The letters
a,e,i,o, andu, in either uppercase or lowercase, are considered vowels. - If there's an odd number of characters in the string, ignore the center character.
--hints--
isBalanced("racecar") should return true.
assert.isTrue(isBalanced("racecar"));
isBalanced("Lorem Ipsum") should return true.
assert.isTrue(isBalanced("Lorem Ipsum"));
isBalanced("Kitty Ipsum") should return false.
assert.isFalse(isBalanced("Kitty Ipsum"));
isBalanced("string") should return false.
assert.isFalse(isBalanced("string"));
isBalanced(" ") should return true.
assert.isTrue(isBalanced(" "));
isBalanced("abcdefghijklmnopqrstuvwxyz") should return false.
assert.isFalse(isBalanced("abcdefghijklmnopqrstuvwxyz"));
isBalanced("123A#b!E&*456-o.U") should return true.
assert.isTrue(isBalanced("123A#b!E&*456-o.U"));
--seed--
--seed-contents--
function isBalanced(s) {
return s;
}
--solutions--
function isBalanced(s) {
const vowels = 'aeiou';
const half = Math.floor(s.length / 2);
let firstHalf = s.slice(0, half);
let secondHalf = s.length % 2 === 0 ? s.slice(half) : s.slice(half + 1);
const countVowels = str =>
str
.toLowerCase()
.split('')
.filter(c => vowels.includes(c))
.length;
return countVowels(firstHalf) === countVowels(secondHalf);
}