function checkGrammar() {
const input = document.getElementById("input-text").value;
const errors = [];
// split input text into sentences
const sentences = input.match(/[^\.!\?]+[\.!\?]+/g);
// loop through each sentence
sentences.forEach(sentence => {
// split sentence into words
const words = sentence.split(' ');
// loop through each word
words.forEach(word => {
// check if word is misspelled
if (misspelled(word)) {
errors.push(word);
}
});
});
// display result
if (errors.length === 0) {
document.getElementById("result").innerHTML = "No grammar errors found.";
} else {
const errorString = errors.join(', ');
document.getElementById("result").innerHTML = "Errors: " + errorString;
}
}
function misspelled(word) {
// function to check if word is misspelled
// you can use an API or library like Spellcheck API or Grammarly API to check spelling and grammar
// or you can create your own function to check for common mistakes
// for example, check if word starts with a capital letter and is not the first word in a sentence
const firstChar = word.charAt(0);
if (firstChar === firstChar.toUpperCase() && !word.endsWith('.') && !word.endsWith('?') && !word.endsWith('!')) {
return true;
}
return false;
}