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. 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!`
JavaScript
x
84
84
1
const form = document.getElementById('form');
2
form.addEventListener('submit', (event) => {
3
// Prevent the default form submission behavior
4
event.preventDefault();
5
6
// Get the original text and the copy from the form
7
const originalText = form.elements.originalText.value.trim();
8
const copy = form.elements.copy.value.trim();
9
10
// Compare the original text and the copy
11
if (originalText === copy) {
12
alert('The texts are the same!');
13
} else {
14
// Display the differences between the two texts
15
const differencesDiv = document.getElementById('result');
16
differencesDiv.innerHTML = '';
17
18
// Split the texts into arrays of sentences
19
const originalSentences = originalText.split('. ');
20
const copySentences = copy.split('. ');
21
22
// Create a table element
23
const table = document.createElement('table');
24
table.classList.add('differences-table');
25
26
// Create a row for the titles
27
const titlesRow = document.createElement('tr');
28
const originalTitleCell = document.createElement('th');
29
originalTitleCell.innerText = 'Original';
30
const copyTitleCell = document.createElement('th');
31
copyTitleCell.innerText = 'New Version';
32
33
// Append the title cells to the titles row
34
titlesRow.appendChild(originalTitleCell);
35
titlesRow.appendChild(copyTitleCell);
36
37
// Append the titles row to the table
38
table.appendChild(titlesRow);
39
40
// Compare the sentences in the texts
41
for (let i = 0; i < originalSentences.length; i++) {
42
// Create a row for the sentence
43
const row = document.createElement('tr');
44
45
// Create cells for the original and copy sentences
46
const originalCell = document.createElement('td');
47
originalCell.innerHTML = originalSentences[i];
48
const copyCell = document.createElement('td');
49
copyCell.innerHTML = copySentences[i];
50
51
// Set the API endpoint and your API key
52
const apiEndpoint = 'https://api.webspellchecker.net/v2/spell-check-text';
53
const apiKey = 'MY API KEY';
54
55
// Check the spelling of the words in the original sentence
56
fetch(apiEndpoint, {
57
method: 'POST',
58
headers: {
59
'Content-Type': 'application/x-www-form-urlencoded',
60
'X-Api-Key': apiKey
61
},
62
body: `text=${encodeURIComponent(originalSentences[i])}`
63
})
64
.then(response => response.json())
65
.then(data => {
66
// data.result.spellCheck.errors contains an array of spelling errors in
67
// the original sentence
68
data.result.spellCheck.errors.forEach(error => {
69
// Add the span element with the different class to the original cell
70
originalCell.innerHTML = originalCell.innerHTML.replace(error.word, `<span class="different">${error.word}</span>`);
71
});
72
});
73
74
// Append the cells to the row
75
row.appendChild(originalCell);
76
row.appendChild(copyCell);
77
78
// Append the row to the table
79
table.appendChild(row);
80
} // Closing curly bracket for the for loop
81
82
83
84
Advertisement
Answer
There are several issues with your code:
- Your else condition has no closing curly bracket (‘}’)
- It is missing a closing parenthesis and curly bracket for the event listener
Corrected code:
JavaScript
1
84
84
1
const form = document.getElementById('form');
2
form.addEventListener('submit', (event) => {
3
// Prevent the default form submission behavior
4
event.preventDefault();
5
6
// Get the original text and the copy from the form
7
const originalText = form.elements.originalText.value.trim();
8
const copy = form.elements.copy.value.trim();
9
10
// Compare the original text and the copy
11
if (originalText === copy) {
12
alert('The texts are the same!');
13
} else {
14
// Display the differences between the two texts
15
const differencesDiv = document.getElementById('result');
16
differencesDiv.innerHTML = '';
17
18
// Split the texts into arrays of sentences
19
const originalSentences = originalText.split('. ');
20
const copySentences = copy.split('. ');
21
22
// Create a table element
23
const table = document.createElement('table');
24
table.classList.add('differences-table');
25
26
// Create a row for the titles
27
const titlesRow = document.createElement('tr');
28
const originalTitleCell = document.createElement('th');
29
originalTitleCell.innerText = 'Original';
30
const copyTitleCell = document.createElement('th');
31
copyTitleCell.innerText = 'New Version';
32
33
// Append the title cells to the titles row
34
titlesRow.appendChild(originalTitleCell);
35
titlesRow.appendChild(copyTitleCell);
36
37
// Append the titles row to the table
38
table.appendChild(titlesRow);
39
40
// Compare the sentences in the texts
41
for (let i = 0; i < originalSentences.length; i++) {
42
// Create a row for the sentence
43
const row = document.createElement('tr');
44
45
// Create cells for the original and copy sentences
46
const originalCell = document.createElement('td');
47
originalCell.innerHTML = originalSentences[i];
48
const copyCell = document.createElement('td');
49
copyCell.innerHTML = copySentences[i];
50
51
// Set the API endpoint and your API key
52
const apiEndpoint = 'https://api.webspellchecker.net/v2/spell-check-text';
53
const apiKey = 'MY API KEY';
54
55
// Check the spelling of the words in the original sentence
56
fetch(apiEndpoint, {
57
method: 'POST',
58
headers: {
59
'Content-Type': 'application/x-www-form-urlencoded',
60
'X-Api-Key': apiKey
61
},
62
body: `text=${encodeURIComponent(originalSentences[i])}`
63
})
64
.then(response => response.json())
65
.then(data => {
66
// data.result.spellCheck.errors contains an array of spelling errors in
67
// the original sentence
68
data.result.spellCheck.errors.forEach(error => {
69
// Add the span element with the different class to the original cell
70
originalCell.innerHTML = originalCell.innerHTML.replace(error.word, `<span class="different">${error.word}</span>`);
71
});
72
});
73
74
// Append the cells to the row
75
row.appendChild(originalCell);
76
row.appendChild(copyCell);
77
78
// Append the row to the table
79
table.appendChild(row);
80
}
81
82
}// Closing curly bracket for the for loop
83
})
84