| Viewed 59 times

How to Return the Index of All Capital Letters in javaScript?


I need to create a function that takes a single string as an argument and returns an ordered array containing the indices of all capital letters in the string.

1 Answer
Mohammad Qandeel
Answered
16

This function returns exactly what is required

function indexOfCaps(str) {
  var res = [];
  for(var i = 0; i < str.length; i++){
    if (/[A-Z]/.test(str[i])){
      res.push(i);
    }
  }
  return res;
}

Example

indexOfCaps('AbCD') //  [0, 2, 3]



Related Questions