用 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

更新時間:23-11-2020

205 次瀏覽

啟動您的 職業

完成課程獲得認證

入門
廣告
© . All rights reserved.