Question: function validateSentence ( str ) { try { / / Define vowels and split the sentence into words var vowels = new Set ( [
function validateSentencestr
try
Define vowels and split the sentence into words
var vowels new Setaeiou;
var words strtrimsplits;
Handle empty input
if wordslength
return false;
for var i ; i words.length; i
var word wordsireplaceazAZg; Remove nonalphabetic characters
Check if word contains alphabetic characters and begins with a vowel
if wordlength
continue; Ignore empty words after cleaning
if vowelshaswordcharAttoLowerCase
console.logWord starts with vowel: word;
return false;
If no word starts with a vowel, the sentence is valid
return true;
catch err
document.getElementByIdresultinnerHTML "Function validateSentence: err;
return false;
function getScorestr char
try
Split the sentence into words and count the number of words starting with char
var words strtrimsplits;
var correctWords ;
var incorrectWords ;
Handle empty input
if wordslength
return ;
for var i ; i words.length; i
var word wordsireplaceazAZg; Remove nonalphabetic characters
if wordlength
continue; Ignore empty words
if wordcharAttoLowerCase char.toLowerCase
correctWords;
else
incorrectWords; Track words that do not start with the given character
If no words start with the given char, return score
if correctWords
return ;
var score ;
Calculate score based on the number of correct words
if correctWords
score ;
else
score correctWords ;
console.logCorrect words: correctWords Incorrect words: incorrectWords;
return score;
catch err
document.getElementByIdresultinnerHTML "Function getScore: err;
return ;
Please check the logic of getScore method for a valid sentence with incorret word
Check the logic of the validateSentence method for a valid sentence
Give alternate code to pass the testcases or fix the issue
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
