用 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP