0
Find the total Caps in an array of strings - JavaScript
Hello coders, I want to write a function called totalCaps, which accepts an array of strings and returns the total number of capitals in each of the strings. So far, I've used .includes method and used a counter variable to store the count of capital letters. My output is showing 0. Could you tell me what's wrong in the logic? Thanks! https://code.sololearn.com/WNrYjKD2ymxI
2 odpowiedzi
0
You are checking if the string with the capital letters include "Hello" or "World", which may or may not be true. I think you just need to replace arr[i] with arr[i][0], so it checks the first letter.
0
If you trying to find "total number of capitals" you need to use two loops, the outer loop loops through the array (the stings) and an inner loop to loop through each character in the strings.
function totalCaps (arr) {
let count=0;
let capWords = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i=0; i<arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
if (capWords.includes(arr[i][j])) {
count ++;
}
}
}
return count;
}