Replace With Alphabet Position ( 6 kyu )

문제

Welcome.

In this kata you are required to, given a string, replace every letter with its position in the alphabet.

If anything in the text isn’t a letter, ignore it and don’t return it.

a being 1, b being 2, etc.

As an example:

alphabet_position("The sunset sets at twelve o' clock.")

Should return “20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11” as a string.

알파벳을 숫자로 치환해 리턴하는 문제

풀이

function alphabetPosition(text) {
  let result = '';
  let arr = text.toLowerCase().split(' ').join('').split('');
  for ( let i = 0; i < arr.length; i++ ) {
    if ( arr[i].charCodeAt(0) >= 97 && arr[i].charCodeAt(0) <= 122 ) {
      result += arr[i].charCodeAt(0)-96 + ' '
    }
  }
  return result.substring(0, result.length-1);
}

아스키코드로 변환한 뒤에 96만큼 빼준 값을 문자열에 추가

다른 사람의 풀이

똑같음


+ Recent posts