본문 바로가기
알고리즘/Codewars

[Codewars] Find the capitals (7 kyu) / JavaScript

by fluss 2023. 3. 25.

https://www.codewars.com/kata/539ee3b6757843632d00026b

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Instructions

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.

 

Example

Test.assertSimilar( capitals('CodEWaRs'), [0,3,4,6] );

 

코드

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

댓글