Instructions:
Write a function that takes a single non-empty string of only lowercase and uppercase ascii letters (word) as its argument, and returns an ordered list containing the indices of all capital (uppercase) letters in the string.
Example (Input --> Output)
"CodEWaRs" --> [0,3,4,6]
Solution:
var capitals = function (word) {
// Write your code here
const indexes = [];
for (let i = 0; i < word.length; i++) {
if (word[i] === word[i].toUpperCase()) {
indexes.push(i);
}
}
return indices;
}
Thoughts:
- I initialize the indexes as an array and loop through the string word with for loop.
- If the letter ( index of the word) matches the condition, the index will be addes to the indexes array.
Top comments (0)