繁体   English   中英

语法错误 - 即使我已经找到了错误,但错误仍然存在

[英]Sytax error - Even though I have already found the bug but the error still there

我正在创建一个发现差异并检查拼写的应用程序。 在为“for”循环添加缺少的大括号后,我遇到了同样的错误。 我不确定为什么它会重复并且错误不会消失。

未捕获的语法错误:输入意外结束

如果我做错了什么,请告诉我。

谢谢!`


const form = document.getElementById('form');
form.addEventListener('submit', (event) => {
  // Prevent the default form submission behavior
  event.preventDefault();

  // Get the original text and the copy from the form
  const originalText = form.elements.originalText.value.trim();
  const copy = form.elements.copy.value.trim();

  // Compare the original text and the copy
  if (originalText === copy) {
    alert('The texts are the same!');
  } else {
    // Display the differences between the two texts
    const differencesDiv = document.getElementById('result');
    differencesDiv.innerHTML = '';

    // Split the texts into arrays of sentences
    const originalSentences = originalText.split('. ');
    const copySentences = copy.split('. ');

    // Create a table element
    const table = document.createElement('table');
    table.classList.add('differences-table');

    // Create a row for the titles
    const titlesRow = document.createElement('tr');
    const originalTitleCell = document.createElement('th');
    originalTitleCell.innerText = 'Original';
    const copyTitleCell = document.createElement('th');
    copyTitleCell.innerText = 'New Version';

    // Append the title cells to the titles row
    titlesRow.appendChild(originalTitleCell);
    titlesRow.appendChild(copyTitleCell);

    // Append the titles row to the table
    table.appendChild(titlesRow);

       // Compare the sentences in the texts
    for (let i = 0; i < originalSentences.length; i++) {
      // Create a row for the sentence
      const row = document.createElement('tr');

      // Create cells for the original and copy sentences
      const originalCell = document.createElement('td');
      originalCell.innerHTML = originalSentences[i];
      const copyCell = document.createElement('td');
      copyCell.innerHTML = copySentences[i];

      // Set the API endpoint and your API key
      const apiEndpoint = 'https://api.webspellchecker.net/v2/spell-check-text';
      const apiKey = 'MY API KEY';

      // Check the spelling of the words in the original sentence
     fetch(apiEndpoint, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'X-Api-Key': apiKey
  },
  body: `text=${encodeURIComponent(originalSentences[i])}`
})
.then(response => response.json())
.then(data => {
  // data.result.spellCheck.errors contains an array of spelling errors in
  // the original sentence
  data.result.spellCheck.errors.forEach(error => {
    // Add the span element with the different class to the original cell
    originalCell.innerHTML = originalCell.innerHTML.replace(error.word, `<span class="different">${error.word}</span>`);
  });
});

// Append the cells to the row
row.appendChild(originalCell);
row.appendChild(copyCell);

// Append the row to the table
table.appendChild(row);
} // Closing curly bracket for the for loop



您的代码存在几个问题:

  1. 您的 else 条件没有右花括号 ('}')
  2. 它缺少事件侦听器的右括号和大括号

更正后的代码:

   const form = document.getElementById('form');
   form.addEventListener('submit', (event) => {
    // Prevent the default form submission behavior
    event.preventDefault();

   // Get the original text and the copy from the form
   const originalText = form.elements.originalText.value.trim();
   const copy = form.elements.copy.value.trim();

    // Compare the original text and the copy
    if (originalText === copy) {
      alert('The texts are the same!');
    } else {
    // Display the differences between the two texts
    const differencesDiv = document.getElementById('result');
    differencesDiv.innerHTML = '';

    // Split the texts into arrays of sentences
    const originalSentences = originalText.split('. ');
    const copySentences = copy.split('. ');

    // Create a table element
    const table = document.createElement('table');
    table.classList.add('differences-table');

    // Create a row for the titles
    const titlesRow = document.createElement('tr');
    const originalTitleCell = document.createElement('th');
    originalTitleCell.innerText = 'Original';
    const copyTitleCell = document.createElement('th');
    copyTitleCell.innerText = 'New Version';

    // Append the title cells to the titles row
    titlesRow.appendChild(originalTitleCell);
    titlesRow.appendChild(copyTitleCell);

    // Append the titles row to the table
    table.appendChild(titlesRow);

       // Compare the sentences in the texts
    for (let i = 0; i < originalSentences.length; i++) {
      // Create a row for the sentence
      const row = document.createElement('tr');

      // Create cells for the original and copy sentences
      const originalCell = document.createElement('td');
      originalCell.innerHTML = originalSentences[i];
      const copyCell = document.createElement('td');
      copyCell.innerHTML = copySentences[i];

      // Set the API endpoint and your API key
      const apiEndpoint = 'https://api.webspellchecker.net/v2/spell-check-text';
      const apiKey = 'MY API KEY';

      // Check the spelling of the words in the original sentence
     fetch(apiEndpoint, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'X-Api-Key': apiKey
  },
  body: `text=${encodeURIComponent(originalSentences[i])}`
})
.then(response => response.json())
.then(data => {
  // data.result.spellCheck.errors contains an array of spelling errors in
  // the original sentence
  data.result.spellCheck.errors.forEach(error => {
    // Add the span element with the different class to the original cell
    originalCell.innerHTML = originalCell.innerHTML.replace(error.word, `<span class="different">${error.word}</span>`);
  });
});

    // Append the cells to the row
    row.appendChild(originalCell);
    row.appendChild(copyCell);

    // Append the row to the table
    table.appendChild(row);
}

}// Closing curly bracket for the for loop
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM