循环重复添加字符“!?。”,即使它不应该

The loop repeats adding characters "!?.," even though it shouldn't

我想创建一个简单的函数,将句子转换为北约音标。该循环适用于字母表,但会产生一个奇怪的字符问题。我的代码:

const alphabet = ["Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", 
"Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", 
"Uniform", "Victor", "Whiskey", "Xray", "Yankee", "Zulu"];

function phoneticAlph(words) {
let translatedTxt = "";
for (let i = 0; i < words.length; i++) {
    for (let x = 0; x < alphabet.length; x++) {
        if (words[i].toUpperCase() === alphabet[x][0]) {
            translatedTxt += `${alphabet[x]} `;
        } else if (words[i] === "!" || words[i] === "?" || words[i] === "," || words[i] === ".") {
            translatedTxt += words[i];
        }
     }
 }
 console.log(translatedTxt)
}

phoneticAlph("!I.a,m!Robot")

这出现在控制台中:

!!!!!!!!!!!!!!!!!!!!!!!!!!India ..........................Alfa ,,,,,,,,,,,,,,,,,,,,,,,,,,Mike 
!!!!!!!!!!!!!!!!!!!!!!!!!!Romeo Oscar Bravo Oscar Tango

我完全不明白发生了什么或如何解决它以正确显示字符。我将非常感谢你的帮助和解释实际发生的事情。

命中可以继续外循环,else case必须移到外循环:

    const alphabet = ["Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", 
    "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", 
    "Uniform", "Victor", "Whiskey", "Xray", "Yankee", "Zulu"];

    function phoneticAlph(words) {
    let translatedTxt = "";
    outer: for (let i = 0; i < words.length; i++) {
        for (let x = 0; x < alphabet.length; x++) {
            if (words[i].toUpperCase() === alphabet[x][0]) {
                translatedTxt += `${alphabet[x]} `;
                continue outer;
            }
         }
         if (words[i] === "!" || words[i] === "?" || words[i] === "," || words[i] === ".") {
                translatedTxt += words[i];
         }
     }
     console.log(translatedTxt)
    }

    phoneticAlph("!I.a,m!Robot")

似乎在第二个循环中缺少 break 语句,这意味着即使其中一个 if 语句为真,循环也会继续。尝试在 if 语句中添加 break 语句,以便在字母表数组中找到所需内容时停止循环。

问题出在第二个for循环中的else if。对于每个特殊字符,第一个 if 总是被跳过,而不是 else if 总是被命中。将 else if 部分移到第二个循环之外 for.

const alphabet = ["Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", 
"Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", 
"Uniform", "Victor", "Whiskey", "Xray", "Yankee", "Zulu"];

function phoneticAlph(words) {
let translatedTxt = "";
for (let i = 0; i < words.length; i++) {
    for (let x = 0; x < alphabet.length; x++) {
        if (words[i].toUpperCase() === alphabet[x][0]) {
            translatedTxt += `${alphabet[x]} `;
        }
     }
     if (words[i] === "!" || words[i] === "?" || words[i] === "," || words[i] === ".") {
        translatedTxt += `${words[i]} ` ;
    }
 }
 console.log(translatedTxt)
}

phoneticAlph("!I.a,m!Robot")

我是 javaScript.but 的新手 我想我修复了 problem.First,您必须从字母以外的字符中删除“单词”。接下来的代码还是一样。

 function phoneticAlph(words) {
 let translatedTxt = "";
 let cleanWords=words.match(/\w/g);              //only letters
 for (let i = 0; i < cleanWords.length; i++) {
   for (let x = 0; x < alphabet.length; x++) {
    if (cleanWords[i].toUpperCase() === alphabet[x][0]) {
        translatedTxt += `${alphabet[x]} `;
    } else if (cleanWords[i] === "!" || cleanWords[i] === "?" || cleanWords[i] === "," 
  || cleanWords[i] === ".") {
        translatedTxt += words[i];
      }
    }
   }
  console.log(translatedTxt)
  }

      phoneticAlph("!I.a,m!Robot")