codewars Titlecase

samuel Jo·2022년 11월 18일
0

codewars

목록 보기
3/46

// A string is considered to be in title case if each word in the string is either (a) capitalised (that is, only the first letter of the word is in upper case) or (b) considered to be an exception and put entirely into lower case unless it is the first word, which is always capitalised.

// Write a function that will convert a string into title case, given an optional list of exceptions (minor words). The list of minor words will be given as a string with each word separated by a space. Your function should ignore the case of the minor words string -- it should behave in the same way even if the case of the minor word string is changed.

// Arguments (Haskell)
// First argument: space-delimited list of minor words that must always be lowercase except for the first word in the string.
// Second argument: the original string to be converted.
// Arguments (Other languages)
// First argument (required): the original string to be converted.
// Second argument (optional): space-delimited list of minor words that must always be lowercase except for the first word in the string. The JavaScript/CoffeeScript tests will pass undefined when this argument is unused.

function titleCase(title, minorWords) {
  if (title.length === 0) return title;
  var words = title.toLowerCase().split(" ");
  var minorWordsArray = minorWords ? minorWords.toLowerCase().split(" ") : [];
  return words
    .map(function (word, index) {
      if (minorWordsArray.indexOf(word) !== -1 && index != 0) {
        return word;
      }
      return word.charAt(0).toUpperCase() + word.slice(1);
    })
    .join(" ");
}
console.log(titleCase("a clash of KINGS", "a an the of"));
console.log(titleCase("THE WIND IN THE WILLOWS", "The In")); // should return: 'The Wind in the Willows'
console.log(titleCase("the quick brown fox")); // should return: 'The Quick Brown Fox'

문제가 이해가 가지않아서 가만히앉아서 1시간을 읽었고,,, 총 걸린시간은 1시간 반정도 걸렸다... 리뷰를 보니 문제 설명이 난해하다는 얘기가 많다.

profile
step by step

0개의 댓글