Question: function checkAlliteration ( ) { try { / / Get the value of char & alliter components var char = document.getElementById ( char

function checkAlliteration(){
try {
// Get the value of char & alliter components
var char = document.getElementById("char").value.toLowerCase();
var sentence = document.getElementById("alliter").value.toLowerCase();
// Invoke getCount() method to get the number of words
var wordCount = getCount(sentence);
if (wordCount <3){
document.getElementById("result").innerHTML = "Invalid number of words";
return;
}
// Invoke validateSentence() method to check if the sentence is valid
var isValid = validateSentence(sentence);
if (!isValid){
document.getElementById("result").innerHTML = "Invalid sentence";
return;
}
// Invoke getScore() method to calculate the score
var score = getScore(sentence, char);
document.getElementById("result").innerHTML = "Your score is "+ score;
} catch (err){
document.getElementById("result").innerHTML = "Error in checkAlliteration: "+ err;
}
}
function getCount(str){
try {
// Calculates the number of words in the string and returns the count
var count =0;
var isWord = false;
for (var i =0; i < str.length; i++){
if (str[i]!==''){
if (!isWord){
isWord = true;
count++;
}
} else {
isWord = false;
}
}
return count;
} catch (err){
document.getElementById("result").innerHTML = "Function getCount: "+ err;
return 0;
}
}
function validateSentence(str){
try {
// When any word in the string starts with a vowel, return false; else, return true
var vowels = new Set(['a','e','i','o','u']);
var words = str.trim().split(/\s+/);
for (var i =0; i < words.length; i++){
var word = words[i].replace(/[^a-zA-Z]+/g,'');
if (word.length ===0|| vowels.has(word.charAt(0).toLowerCase())){
return false;
}
}
return true;
} catch (err){
document.getElementById("result").innerHTML = "Function validateSentence: "+ err;
return false;
}
}
function getScore(str, char){
try {
// Compare the first letter of every word in the string with the character, calculate and return score
var words = str.split(/\s+/);
var correctWords =0;
for (var i =0; i < words.length; i++){
var word = words[i].replace(/[^a-zA-Z]+/g,'');
if (word.length >0 && word.charAt(0).toLowerCase()=== char.toLowerCase()){
correctWords++;
}
}
var score =0;
if (correctWords >0){
score = correctWords <=3?2 : 2+(correctWords -3)*2;
}
return score;
} catch (err){
document.getElementById("result").innerHTML = "Function getScore: "+ err;
return 0;
}
}
Please check the logic for getScore method for a valid sentence with incorret word
Check the logic of the validateSentence method for a valid sentence
How to fix it

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!