Code Wars: Find the capitals
Write a function that takes a single 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; };
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
Post a Comment