Search code examples
javascriptfunctionloopsconditional-statementscapitalize

Count the capitals in the word javascript


) I am trying to solve one problem with this statement :

Write a function called howManyCaps which counts the capitals in the word,it then returns a sentence saying how which letters are capital and how many capitals there are in total.

This is my function

function howManyCaps(str) {
  var count = 0;
  for (i = 0; i < str.length; i++) {
    if (str[i] == str[i].toUpperCase()) {
      console.log(true);
      count++;
    } else {
      false;
    }
  }
  return count;
}

but in console if try with something like str=" How many Caps" y see a value of 5 instead of 2. Any suggestions? Thanks


Solution

  • Problem is your spacing.

    " How many Caps"
    

    has 3 spaces and 2 capital letters.. that should help you debug