Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript에서 알파벳을 뒤에서 앞으로, 앞에서 뒤로 뒤집기


단일 알파벳을 유일한 입력으로 사용하는 JavaScript 함수를 작성해야 합니다. 이 함수는 해당 알파벳의 시작 위치를 계산하고 같은 위치에 있지만 뒤에서 오는 알파벳을 반환해야 합니다.

예시

이에 대한 코드는 -

const alpha = 'g';
const findCounterPart = (alpha = '') => {
   let alphabet = 'abcdefghijklmnopqrstuvwxyz';
   let firstpart = alphabet.substring(0,13).split('');
   let secondpart = alphabet.substring(13).split('').reverse();
   let solution = '';
   if (firstpart.indexOf(alpha) !== −1) {
      solution = secondpart[firstpart.indexOf(alpha)];
   } else {
      solution = firstpart[secondpart.indexOf(alpha)];
   };
   return solution;
};
console.log(findCounterPart(alpha));

출력

콘솔의 출력은 -

t