我编写了一个 JavaScript 函数,它接受两个未连接的字符串作为参数,palindromeLeft 和 palindromeRight (如果连接,将形成一个回文 - 不是在编程意义上,而是在文学意义上:“吟游诗人循环”/ "pools bard") 并将它们转换为两个由分隔字符组成的数组 (arrayLeft 和 arrayRight),其中添加了空元素,其目的是表明在另一个数组中的等效位置有一个 dummyCharacter (即不是一封信)。
该函数可以访问两个全局变量,dummyCharacters和diacriticVariations,后者对于调试此函数并不那么重要(我相信)。我将下面的代码与 console.logs 一起发布,将返回数组的空元素显示为“X”:
const dummyCharacters = /[.,\/#!$%^&*;:{}=\-_~()`<>\'"\[\]@+?]/g; // Doesn't include space
const diacriticVariations = {
a: ['a', 'à', 'á', 'â', 'ä', 'å', 'ã', 'æ', 'ā', 'ă', 'ą'],
e: ['e', 'è', 'é', 'ê', 'ë', 'ē', 'ĕ', 'ę', 'ě', 'ǝ'],
// etc.
};
// "bard's pool", "loops drab"
function ConstructsPalindromeBlueprint(palindromeLeft, palindromeRight) {
let arrayLeft = [];
let arrayRight = [];
let newLeft = palindromeLeft.split('');
let newRight = palindromeRight.split('').reverse();
while (newLeft.length > 0 || newRight.length > 0) {
if (newLeft[newLeft.length - 1] === newRight[newRight.length - 1]) {
arrayLeft.push(newLeft[newLeft.length - 1]);
arrayRight.push(newRight[newRight.length - 1]);
newLeft.pop();
newRight.pop();
console.log("Left:" + JSON.stringify(arrayLeft.map(e => e === "" ? "X" : e)));
console.log("Right:" + JSON.stringify(arrayRight.map(e => e === "" ? "X" : e)));
continue;
}
if (diacriticVariations[newLeft[newLeft.length - 1]] === diacriticVariations[newRight[newRight.length - 1]]) {
arrayLeft.push(newLeft[newLeft.length - 1]);
arrayRight.push(newRight[newRight.length - 1]);
newLeft.pop();
newRight.pop();
console.log("Left:" + JSON.stringify(arrayLeft.map(e => e === "" ? "X" : e)));
console.log("Right:" + JSON.stringify(arrayRight.map(e => e === "" ? "X" : e)));
continue;
}
if (newLeft[newLeft.length - 1] === ' ') {
arrayLeft.push(newLeft[newLeft.length - 1]);
arrayRight.push('');
newLeft.pop();
console.log("Left:" + JSON.stringify(arrayLeft.map(e => e === "" ? "X" : e)));
console.log("Right:" + JSON.stringify(arrayRight.map(e => e === "" ? "X" : e)));
continue;
}
if (newRight[newRight.length - 1] === ' ') {
arrayRight.push(newRight[newRight.length - 1]);
arrayLeft.push('');
newRight.pop();
console.log("Left:" + JSON.stringify(arrayLeft.map(e => e === "" ? "X" : e)));
console.log("Right:" + JSON.stringify(arrayRight.map(e => e === "" ? "X" : e)));
continue;
}
if (dummyCharacters.test(newLeft[newLeft.length - 1])) {
arrayLeft.push(newLeft[newLeft.length - 1]);
arrayRight.push('');
newLeft.pop();
console.log("Left:" + JSON.stringify(arrayLeft.map(e => e === "" ? "X" : e)));
console.log("Right:" + JSON.stringify(arrayRight.map(e => e === "" ? "X" : e)));
continue;
}
if (dummyCharacters.test(newRight[newRight.length - 1])) {
arrayRight.push(newRight[newRight.length - 1]);
arrayLeft.push('');
newRight.pop();
console.log("Left:" + JSON.stringify(arrayLeft.map(e => e === "" ? "X" : e)));
console.log("Right:" + JSON.stringify(arrayRight.map(e => e === "" ? "X" : e)));
continue;
}
newLeft.pop();
newRight.pop();
}
return [arrayLeft.reverse(), arrayRight];
}
当输入上述一对参数“吟游诗人循环”和“吟游诗人池”时,该函数表现良好,直到 console.log 显示以下迭代:
Left:["l","o","o","p"," ","s","'"] Right:["l","o","o","p","X","s"," "]
出于某种原因,当我的预期目的是让函数识别出撇号是dummyCharacters之一时,函数将arrayLeft推入撇号并推入arrayRight空格,将其推到arrayLeft上,从而将空元素推到arrayRight上,然后在下一次迭代中将space推到arrayRight上,从而将空元素推到arrayLeft上。
有人知道为什么会出现这个错误吗?为什么 while 循环在一次迭代期间将这两个撇号和空格推到arrayLeft和arrayRight上?