This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convToStr (num) { | |
const chars = [] // 設一個空陣列推入算好轉成 acsii 碼後的值 | |
let _n = num | |
while(ture) { // Q: 為何要用while? A: 因為要創一個 loop 讓商數可以繼續除下去,不然永遠只能 return 一個值 | |
if( _n < 26) { | |
chars.push(_n + calcItForIndex) // 小於 26 的值就能直接加 64,再透過 String.fromCharCode() 方法把 ascii 碼轉換成英文字 | |
break // 沒有 break 掉的話,就會一直把 _n 推進去 chars 陣列裡面,形成無窮迴圈 | |
} // 如果值大於 26 就進行下面動作 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const base26 = 26 | |
const calcIfForIndex = 64 // 借用 ascii code 來實現 A(1), B(2), C(3)...Z(26),因為A的 ascii 碼是65,所以固定 - 1 讓我們能自定義 A ~ Z 的 index | |
function convToNumber (str) { | |
const length = str.length // 字串的長度 | |
let n = 0 | |
for (let idx = 0; idx < length; idx++) { // 用 for loop 跑整個輸入的字串,這樣就不用擔心輸入的字串長短的問題了 | |
const c = str.charCodeAt(idx) // 用 charCodeAt 方法把輸入的字串轉成 ascii 碼 | |
n += Math,pow(base26, (length - 1 - idx) * (c - calcItForIndex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let firstInput = document.querySelector('#firstInput') | |
let secondInput = document.querySelector('#secondInput') | |
const btn = document.querySelector('button'); | |
let arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | |
let length = arr.length | |
btn.addEventListener('click', function () { | |
// console.log(firstInput.value[0]); |