Question: function validateSentence ( str ) { try { / / Define vowels and split the sentence into words var vowels = new Set ( [

function validateSentence(str){
try {
// Define vowels and split the sentence into words
var vowels = new Set(['a','e','i','o','u']);
var words = str.trim().split(/\s+/);
// Handle empty input
if (words.length ===0){
return false;
}
for (var i =0; i < words.length; i++){
var word = words[i].replace(/[^a-zA-Z]+/g,''); // Remove non-alphabetic characters
// Check if word contains alphabetic characters and begins with a vowel
if (word.length ===0){
continue; // Ignore empty words after cleaning
}
if (vowels.has(word.charAt(0).toLowerCase())){
console.log("Word starts with vowel: "+ word);
return false;
}
}
// If no word starts with a vowel, the sentence is valid
return true;
} catch (err){
document.getElementById("result").innerHTML = "Function validateSentence: "+ err;
return false;
}
}
function getScore(str, char){
try {
// Split the sentence into words and count the number of words starting with char
var words = str.trim().split(/\s+/);
var correctWords =0;
var incorrectWords =0;
// Handle empty input
if (words.length ===0){
return 0;
}
for (var i =0; i < words.length; i++){
var word = words[i].replace(/[^a-zA-Z]+/g,''); // Remove non-alphabetic characters
if (word.length ===0){
continue; // Ignore empty words
}
if (word.charAt(0).toLowerCase()=== char.toLowerCase()){
correctWords++;
} else {
incorrectWords++; // Track words that do not start with the given character
}
}
// If no words start with the given char, return 0 score
if (correctWords ===0){
return 0;
}
var score =0;
// Calculate score based on the number of correct words
if (correctWords <=3){
score =2;
} else {
score =2+(correctWords -3)*2;
}
console.log("Correct words: "+ correctWords +", Incorrect words: "+ incorrectWords);
return score;
} catch (err){
document.getElementById("result").innerHTML = "Function getScore: "+ err;
return 0;
}
}
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!