最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to count how many consonants are in a string? - Stack Overflow

programmeradmin1浏览0评论

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");

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.

Share Improve this question edited Oct 24, 2017 at 11:03 Zakaria Acharki 67.5k15 gold badges78 silver badges105 bronze badges asked Oct 24, 2017 at 10:44 Milos MilojevicMilos Milojevic 572 silver badges7 bronze badges 2
  • 1 Yeah, that looks pretty close. You want to loop i < str.length, not <=. – Evan Trimboli Commented Oct 24, 2017 at 10:47
  • 1 use && operator instead of || – priya_singh Commented Oct 24, 2017 at 10:50
Add a comment  | 

8 Answers 8

Reset to default 6

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.

You can do

let str = 'asdfghaaa';

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

console.log(result);

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).

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?

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

May be not good for long string.

You need to and your conditions because if you use || the condition always evaluates to true. Your loop should go from 0 to index < str.length.

function consonants(str) {
	var countConsonants = 0;

	for (var i = 0; i < str.length; i++) {
		if (str.charAt(i) !== "a" && str.charAt(i) !== "e" && str.charAt(i) !== "i" 
			&& str.charAt(i) !== "o" && str.charAt(i) !== "u" && str.charAt(i) !== " ") {
			countConsonants++;
		}
  }
    return countConsonants;
}
console.log(consonants("asdfghaaa"));

I had this challenge too here is how i did:

const paragraph = "A quick brow fox is trying to bite me."
paragraph.match(/(?![aeiou])[a-z]/gi, "").length

Source: https://forum.freecodecamp.org/t/regex-for-consonants/282833/4

Here is a working example

  let str="Hello world.";
  let vowel=str.match(/[aeiou]/gi).length;
  let consonant = str.match(/[^aeiou .]/gi).length;
  console.log("Vowel "+vowel+"\n");
  console.log("Vowel "+consonant+"\n");

发布评论

评论列表(0)

  1. 暂无评论