@@ -1001,29 +1001,28 @@ <h2>查碼練習</h2>
10011001 } ) ;
10021002
10031003 function updatePhraseLookup ( ) {
1004- let textToSearch ;
1004+ // The text we want to look up codes for. This should always be the content of the phraseInput.
1005+ const textToSearch = phraseInput . value ;
1006+ // The text we want to compare against for coloring. This is the content of the main input area.
1007+ const textToCompare = textOutput . value ;
10051008 let cursorPosition ;
10061009
1007- // 判斷哪個輸入區塊有焦點
1010+ // Determine which textarea has the cursor to get the correct cursor position.
10081011 if ( phraseInput === document . activeElement ) {
1009- textToSearch = phraseInput . value ;
10101012 cursorPosition = phraseInput . selectionStart ;
10111013 } else if ( textOutput === document . activeElement ) {
1012- textToSearch = textOutput . value ;
10131014 cursorPosition = textOutput . selectionStart ;
10141015 } else {
1015- // 如果都沒有焦點,則使用 phraseInput 的內容
1016- textToSearch = phraseInput . value ;
1017- cursorPosition = 0 ;
1016+ cursorPosition = 0 ; // Default if no focus
10181017 }
10191018
10201019 encodingResults . innerHTML = '' ;
10211020
1022- // 根據游標位置計算查碼區塊的起始索引
1021+ // The starting index for the 10-character lookup block, based on the cursor position.
10231022 const lookupStartIndex = Math . floor ( cursorPosition / 10 ) * 10 ;
10241023 const characters = textToSearch . split ( '' ) . slice ( lookupStartIndex , lookupStartIndex + 10 ) ;
10251024
1026- // 固定顯示10個欄位
1025+ // Fixed loop to display 10 columns
10271026 for ( let i = 0 ; i < 10 ; i ++ ) {
10281027 const column = document . createElement ( 'div' ) ;
10291028 column . className = 'char-encoding-column' ;
@@ -1034,15 +1033,17 @@ <h2>查碼練習</h2>
10341033 if ( i < characters . length ) {
10351034 const char = characters [ i ] ;
10361035 charLabel . textContent = char ;
1037- const results = currentReverseCodemap [ char ] ;
1036+ const globalIndex = lookupStartIndex + i ;
10381037
1039- if ( results && results . length > 0 ) {
1038+ if ( globalIndex < textToCompare . length && char === textToCompare [ globalIndex ] ) {
1039+ // The character at this position matches the typed character.
10401040 charLabel . classList . add ( 'correct-char' ) ;
10411041 } else {
1042+ // The character doesn't match or hasn't been typed yet.
10421043 charLabel . classList . add ( 'incorrect-char' ) ;
10431044 }
10441045 }
1045-
1046+
10461047 const encodingContainer = document . createElement ( 'div' ) ;
10471048 encodingContainer . style . display = 'flex' ;
10481049 encodingContainer . style . flexDirection = 'column' ;
0 commit comments