Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count how many consonants are in a string?

Tags:

javascript

Am I somewhere near the solution? I always get a double number of the consonants.

I was convinced that this was the correct approach.

function consonants(str) {
  var countConsonants = 0;

  for (var i = 0; i <= str.length; i++) {

    if (str[i] !== "a" || str[i] !== "e" || str[i] !== "i" ||
      str[i] !== "o" || str[i] !== "u" || str[i] !== " ") {
      countConsonants += 1;
    }
  }
  return (countConsonants);
}
consonants("asdfghaaa");

I am expecting an answer of 5 i.e. sdfgh are the consonants.

like image 790
Milos Milojevic Avatar asked Oct 24 '17 10:10

Milos Milojevic


5 Answers

Your logic is flawed, The operator in your condition should be AND && not OR ||, since you want to compare all the chars not just one of them :

function consonants(str) {
  var countConsonants = 0;

  for (var i = 0; i < str.length; i++) {

    if (str[i] !== "a" && str[i] !== "e" && str[i] !== "i" &&
      str[i] !== "o" && str[i] !== "u" && str[i] !== " ") {
      countConsonants++;
    }
  }
  return (countConsonants);
}
console.log(consonants("asdfghaaa"));

NOTE : The loop should stop at length-1 since arrays are 0 based, so replace :

for (var i = 0; i <= str.length; i++) {
__________________^^

By :

for (var i = 0; i < str.length; i++) {
__________________^

Hope this helps.

like image 138
Zakaria Acharki Avatar answered Nov 18 '22 14:11

Zakaria Acharki


The main problem in counts lies within your conditions.

You're increasing the number of consonants whenever one of the conditions fail (that's what || does, known as the OR operator). So whenever a character !== "a" OR !== "e", you're increasing the count, which is wrong. (Imagine that a is !== 'e', so you're counting a as a consonant).

Change the || binary operator to && (AND); this way, you're only increasing the consonant count whenever the current character str[i] is not among the values you're verifying for (a, e, i, o, u, ' ').

As others pointed out, you're also likely to run into an error as the max value of i should be Length-1.

There also other problems you need to consider:

  • What happens when the character is an uppercase letter?
  • What happens when the character is a punctuation mark or a number?

For a beginner, this may not be relevant, but it's well worth getting these techniques under your skin: It's more readable to create an array that contains all of the value you're verifying for ["a","e", etc] and then, for each char in the source string, just verify if array.indexOf(str[i]) >= 0 (which means that the character is included in the array).

like image 25
devadviser Avatar answered Nov 18 '22 14:11

devadviser


Your answer is almost right. The only problem is || instead of &&. You're checking that it's not a AND it's not e AND it's not i, etc. Your function comes out true for every letter, since a is (not a || not e), right?

like image 21
TKoL Avatar answered Nov 18 '22 14:11

TKoL


You can do

let str = 'asdfghaaa';

let result = str.split('').filter(e => e.match(/[^aeiou]/) != null).length;

console.log(result);
like image 2
marvel308 Avatar answered Nov 18 '22 15:11

marvel308


function consonants (str) {
    return str.match(/[aeoiu]/gi)||[].length;
}

May be not good for long string.

like image 1
mashi Avatar answered Nov 18 '22 14:11

mashi