简体   繁体   English

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

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

I am creating an app that finds the difference and checks for spelling.我正在创建一个发现差异并检查拼写的应用程序。 I am getting the same error after adding the missing curly bracket for the 'for' loop.在为“for”循环添加缺少的大括号后,我遇到了同样的错误。 I am not sure why it is repeating and the error is not going away.我不确定为什么它会重复并且错误不会消失。

Uncaught SyntaxError: Unexpected end of input未捕获的语法错误:输入意外结束

Please let me know if I am doing something wrong.如果我做错了什么,请告诉我。

Thank you!`谢谢!`


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



There are several issues with your code:您的代码存在几个问题:

  1. Your else condition has no closing curly bracket ('}')您的 else 条件没有右花括号 ('}')
  2. It is missing a closing parenthesis and curly bracket for the event listener它缺少事件侦听器的右括号和大括号

Corrected code:更正后的代码:

   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.

相关问题 Sinon存根错误:即使我正在还原,也“尝试包装已经包装的帖子” - Sinon stub error: “Attempted to wrap post which is already wrapped” even though I'm restoring 为什么即使我已经在其中添加了一些显示错误消息,零值仍然可以提交? - Why the zero value still can submit even I already add some display error message there? 即使只有一个参数,我也会收到“包装的保证不是可迭代的错误” - I'm getting a 'Wrapped promise not iterable error' even though I have only one parameter 即使我的代码中有catch部分,我仍收到未处理的Promise Rejection错误 - I am getting Unhandled Promise Rejection error even though I have the catch section in my code 即使我没有错误,我的模拟时钟处理 java 脚本也不工作 - My analog clock handles java script not working even though I have no error 使用redmine API进行POST请求时出现401错误,即使我已经包含了api密钥 - Getting 401 error when using redmine API for a POST request even though I have included the api key this.setState is not a function 错误仍然存在,即使我遵循了许多使用相同代码的示例 - this.setState is not a function error is persistent even though I have followed many examples that use the same code 即使我添加了请求模块,也会收到“错误:找不到模块‘请求’” - Getting "Error: Cannot find module 'request'" even though I have added request module 即使我具有身份验证,Facebook JS API上的权限错误也无法发表评论 - Permissions error on Facebook JS API for comments, even though I have the auths 我已经在我的 React 网页中导入了 useState 但它仍然显示编译错误 - I have already imported useState in my React webpage but it is still showing compilation error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM