- 29.5k
- 16
- 45
- 201
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
The check for zero length string can be removed, since the loop won't be run.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
What follows are some advanced techniques that many wouldn't expect a beginner/intermediate-level student to utilize. If you really wanted to shorten this code, you could convert the string to an array with the spread operator and then use array reduction with Array.prototype.reduce()
:
const vowels = ['A', 'E', 'I', 'O', 'U'];
const charInVowels = c => vowels.includes(c.toUpperCase());
const checkChar = (count, c) => charInVowels(c) ? ++count : count;
const vowelCount = string => [...string].reduce(checkChar, 0);
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
—-
P.s.did you intentionally put an auto-comment about off-topic posts as the body of your profile??
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
The check for zero length string can be removed, since the loop won't be run.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
What follows are some advanced techniques that many wouldn't expect a beginner/intermediate-level student to utilize. If you really wanted to shorten this code, you could convert the string to an array with the spread operator and then use array reduction with Array.prototype.reduce()
:
const vowels = ['A', 'E', 'I', 'O', 'U'];
const charInVowels = c => vowels.includes(c.toUpperCase());
const checkChar = (count, c) => charInVowels(c) ? ++count : count;
const vowelCount = string => [...string].reduce(checkChar, 0);
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
The check for zero length string can be removed, since the loop won't be run.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
What follows are some advanced techniques that many wouldn't expect a beginner/intermediate-level student to utilize. If you really wanted to shorten this code, you could convert the string to an array with the spread operator and then use array reduction with Array.prototype.reduce()
:
const vowels = ['A', 'E', 'I', 'O', 'U'];
const charInVowels = c => vowels.includes(c.toUpperCase());
const checkChar = (count, c) => charInVowels(c) ? ++count : count;
const vowelCount = string => [...string].reduce(checkChar, 0);
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
—-
P.s.did you intentionally put an auto-comment about off-topic posts as the body of your profile??
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
The check for zero length string can be removed, since the loop won't be run.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
if( string.length == 0) { return 0; }
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
What follows are some advanced techniques that many wouldn't expect a beginner/intermediate-level student to utilize. If you really wanted to shorten this code, you could convert the string to an array with String.prototype.split()
the spread operator and then use array reduction with Array.prototype.reduce()
:
const vowels = ['A', 'E', 'I', 'O', 'U'];
const charInVowels = c => vowels.includes(c.toUpperCase());
const checkChar = (count, c) => charInVowels(c) ? ++count : count;
const vowelCount = string => string[.split('')..string].reduce(checkChar, 0);
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
if( string.length == 0) { return 0; }
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
If you really wanted to shorten this code, you could convert the string to an array with String.prototype.split()
and then use array reduction with Array.prototype.reduce()
:
const vowels = ['A', 'E', 'I', 'O', 'U'];
const charInVowels = c => vowels.includes(c.toUpperCase());
const checkChar = (count, c) => charInVowels(c) ? ++count : count;
const vowelCount = string => string.split('').reduce(checkChar, 0);
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
The check for zero length string can be removed, since the loop won't be run.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
What follows are some advanced techniques that many wouldn't expect a beginner/intermediate-level student to utilize. If you really wanted to shorten this code, you could convert the string to an array with the spread operator and then use array reduction with Array.prototype.reduce()
:
const vowels = ['A', 'E', 'I', 'O', 'U'];
const charInVowels = c => vowels.includes(c.toUpperCase());
const checkChar = (count, c) => charInVowels(c) ? ++count : count;
const vowelCount = string => [...string].reduce(checkChar, 0);
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
if( string.length == 0) { return 0; }
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
If you really wanted to shorten this code, you could convert the string to an array with String.prototype.split()
and then use array reduction with Array.prototype.reduce()
:
const vowels = ['A', 'E', 'I', 'O', 'U'];
const charInVowels = c => vowels.includes(c.toUpperCase());
const checkChar = (count, c) => charInVowels(c) ? ++count : count;
const vowelCount = string => string.split('').reduce(checkChar, 0);
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
if( string.length == 0) { return 0; }
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
I'm just wondering if there's an easier way to solve this problem without the big blocky code in the
if
statement.
Well, you could put all of those in an array:
const vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
And then the if
condition can be simplified using Array.prototype.includes()
:
if( vowels.includes(v)) {
The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.
You could also include either the uppercase or lowercase letters, and then call String.prototype.toUpperCase()
or String.prototype.toLowerCase()
, though if performance is your goal, then the extra function call might be something to consider.
Additionally, a for...of
loop could be used instead of the regular for
loop, to avoid the need to index into the array.
for( const v of string) {
And the postfix increment operator (i.e. ++
) could be used to increase count
instead of adding 1.
const vowels = ['A', 'E', 'I', 'O', 'U'];
function vowelCount(string) {
if( string.length == 0) { return 0; }
let count = 0;
for( const v of string) {
if( vowels.includes(v.toUpperCase())) {
count++;
}
}
return count;
}
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));
If you really wanted to shorten this code, you could convert the string to an array with String.prototype.split()
and then use array reduction with Array.prototype.reduce()
:
const vowels = ['A', 'E', 'I', 'O', 'U'];
const charInVowels = c => vowels.includes(c.toUpperCase());
const checkChar = (count, c) => charInVowels(c) ? ++count : count;
const vowelCount = string => string.split('').reduce(checkChar, 0);
console.log(vowelCount('kookaburra'));
console.log(vowelCount('sky'));