Code Wars: Find the capitals

Write a function that takes a single string (word) as argument. The function must return an ordered list containing the indexes of all capital letters in the string.

Followed this method:
https://stackoverflow.com/questions/2295657/return-positions-of-a-regex-match-in-javascript

var capitals = function (word) {
let regex = /[A-Z]/g;
  let arr = [];

  while((isMatch = regex.exec(word)) != null) {
    arr.push(isMatch.index);
  }
  return arr;

};

Someone else's easier solution:

var capitals = function (word) { var caps = []; for(var i = 0; i < word.length; i++) { if(word[i].toUpperCase() == word[i]) caps.push(i); } return caps; };

Comments

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)

Code Wars: Find the odd int