1

As the title says I am trying to return the total number of upper case characters in a function that accepts an array of strings.

Here is what I have so far:

function totalCaps(str) {
  var count=0,len=str.length;
  for(var i=0;i<len;i++) {
    if(/[A-Z]/.test(str.charAt(i))) count++;
  }
  return count;
}

console.log(
  totalCaps(["AwesomE", "ThIngs", "hAppEning", "HerE"])
  );

Am I going about this the best way? I'm sure there is an alternative solution as to compared what I am doing.

I am trying to return: 8 for the given array of strings that I introduced at the bottom.

  • 3
    There are many alternatives. How is the "best" one going to be determined? – Heretic Monkey Dec 30 '19 at 15:13
  • Does this answer your question? [Counting upper and lower case characters in a string](https://stackoverflow.com/questions/19171240/counting-upper-and-lower-case-characters-in-a-string) – Liam Dec 30 '19 at 15:19

5 Answers5

4

If the "total number of uppercase characters in an array of strings" is what matters to you, I suppose you could refactor the data a little bit to reduce tasks:

  1. concat strings to 1 long string:
 ["sldkDSgj", "sldTd", "slpiasSPA", ...].join('') // "sldkDSgjsldTdslpiasSPA..."
  1. use String.match to receive number of matches to your pattern:
const pattern = /[A-Z]/g
"sldkDSgjsldTdslpiasSPA...".match(pattern).length // 6
shuk
  • 1,745
  • 1
  • 14
  • 25
0

EDIT (both approaches are O(n2) if you need O(n) see shukar answer)

Here is a quick solution. Since you have an array iterate through each item, then in the next inner loop iterate through characters of the current string and check if the character is in uppercase.

function totalCaps(arr) {
  let count = 0,
    len = arr.length;
  for (let i = 0; i < len; i++) {
    const word = arr[i];
    for (let j = 0; j < word.length; j++) {
      if (word[j].toUpperCase() === word[j]) {
        count++
      }

    }
  }
  return count;
}

console.log(
  totalCaps(["AwesomE", "ThIngs", "hAppEning", "HerE"])
);

Here is a functional approach

function totalCaps(arr) {
  return arr.reduce((acc, cur) => acc + cur.split('').reduce((acc_2, x) => x.toUpperCase() === x ? ++acc_2 : acc_2, 0), 0)
}
console.log(totalCaps(["AwesomE", "ThIngs", "hAppEning", "HerE"]))
EugenSunic
  • 13,162
  • 13
  • 64
  • 86
0
function totalCaps(str) {
    var count = 0;
    str.map(item => {
        for (var i=0; i < item.length; i++) {
            if (item[i] == item[i].toUpperCase()) count ++;
        }
    })
    return count;
}

console.log(
    totalCaps(["AwesomE", "ThIngs", "hAppEning", "HerE"])
);
Tinu Jos K
  • 888
  • 1
  • 8
  • 21
0

Note that your suggested solution handles a single string, not a list of strings. To make it work, you could iterate over the given array:

function totalCaps(arr) {
    var count = 0;
    arr.forEach(str => {
        const len = str.length;
        for (var i=0 ; i < len; i++) {
            if (/[A-Z]/.test(str.charAt(i))) {
                count++;
            }
        }
    });
    return count;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Your code won't actually work, because your function assumes that it is getting a single string, but you are passing an array. However, that is easily fixed, so I won't address it. Here's a simple solution. I'm sure others will post variations and alternatives:

function totalCaps(strAry) {
    return strAry
        .join('') // join all strings into one string
        .split('') // split string into array of characters
        .reduce((accum, ch) => accum + (/[A-Z]/.test(ch) ? 1 : 0), 0);
}

console.log(
    totalCaps(['AwesomE', 'ThIngs', 'hAppEning', 'HerE'])
);