DEV Community

Madalina Pastiu
Madalina Pastiu

Posted on

Find the capitals

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;
  }
Enter fullscreen mode Exit fullscreen mode

Thoughts:

  1. I initialize the indexes as an array and loop through the string word with for loop.
  2. If the letter ( index of the word) matches the condition, the index will be addes to the indexes array.

This is a CodeWars Challenge of 7kyu Rank

Top comments (0)