Skip to content

Commit

Permalink
fix(composeExercise.js): keep same-Cangjie questions; make question a…
Browse files Browse the repository at this point in the history
…dvancing O(1) time

The question 叭 (口金; rc) was inaccessible
due to its Cangjie code being the same as 只 (口金; rc).

This issue is fixed by using an Array for MapCharacterTable.questList.

* MapCharacterTable()
    * make this.questList an array (LIFO)
    * reverse the iteration of `this.questList`-building `for` loop
* MapCharacterTable.prototype.getCharacterAndAlphabet()
    * simplify question advancing by using Array.prototype.pop()
    * overwrite input table with the answer of the current question
      for handling characters with the same Cangjie code within the same stage
  • Loading branch information
IepIweidieng committed Oct 3, 2023
1 parent 86efc7c commit 4a143ba
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions composeExercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
function MapCharacterTable(characterUse) {
var characterArray = characterUse.textContent.split('\n');
var table = this.table = {};
var questList = this.questList = {}
for (var i=0, l=characterArray.length; i<l; i++) {
var questList = this.questList = [];
for (var i = characterArray.length - 1; i >= 0; --i) {
if (characterArray[i]) {
var character = characterArray[i].charAt(0), alphabet = characterArray[i].substr(1);
table[alphabet] = character;
questList[alphabet] = character;
questList.push([character, alphabet]);
}
}
}
Expand All @@ -21,14 +21,9 @@ MapCharacterTable.prototype = {
return undefined;
},
getCharacterAndAlphabet: function (oldAlphabet) {
var list = this.questList;
list[oldAlphabet] = undefined;
for (var i in list) {
if (typeof(list[i]) == 'string' && list[i].length == 1) {
return [list[i], i];
}
}
return false;
var res = this.questList.pop();
this.table[res[1]] = res[0];
return res;
},
findCharacter: function (alphabet) {
return this.table[alphabet]
Expand Down

0 comments on commit 4a143ba

Please sign in to comment.