Here is the question in JavaScript that I’m trying to solve:
What is Alliteration?
Alliteration is described as the repetition of the same consonants at the beginning of words that are adjacent to each other. Exclude vowels. Eg: She sells sea shells. S is a consonant and every word in this sentence starts with s.
Criteria on the game score:
- On creating a sentence exactly with 3 correct words (or) for the first 3 correct words of the sentence, the score is 2.
- For each correct word that occurs after 3 words, a score of 2 is given.
CORRECT WORD: Word that starts with the given alphabet.
INCORRECT WORD: Word that does not start with the given alphabet.
In script.js, provide the implementation for the functions as per the requirements mentioned below:
1. Invoke the getCount() and send the sentence as its parameter. The method will return the number of words. If the number of words in the sentence is <3, display the output statement: Invalid number of words & stop. Else, go ahead.
2. Invoke the validateSentence() and send the sentence string as its parameter. The method will return true / false. When false, display the output statement: Invalid sentence. When true, go ahead.
3. Invoke the getScore() and send the sentence string & the key alphabet as its parameters. The method will return the score. Display the output statement: as shown in the screenshot.
Note: In all 3 cases, display the output in div with id 'result' using .innerHTML.
getCount(str): This function will calculate the number of words in the sentence and returns the count to the checkAlliteration() function.
validateSentence(str): When any word of str starts with a vowel, it is invalid and hence returns false to the checkAlliteration() function. Else, return true.
str: She sells shells - VALID SENTENCE
str: She sells apple - INVALID SENTENCE
getScore(str,char): This method will compare the first letter of every word from the valid sentence with the key alphabet, calculate and return the score to the checkAlliteration() function. The score is 2 for the first 3 correct words and +2 for each correct word after that. Score 0 is returned when a mismatch is found.```
IMPORTANT POINTS:
In js, use the getElementById() or getElementsByName() function to retrieve the value of the html component.
Do not use Jquery commands and use the var keyword instead of let / const to define the js variables.
Do not use ES6 commands.
You cannot place a vowel in the first text box. You cannot click on the button without specifying anything in the text fields. HTML page that's already designed restricts you from doing these.
Remember to convert the inputs obtained through 2 text fields to all-small-case in the checkAlliteration(), before sending them as arguments to the other functions.
Do not use console.log() to print any kind of output/message on the console unless it is mentioned in the given description. In case if it is used for debugging purpose, comment it before performing code evaluation.
Though the code works fine and passess all the basic test cases, after compiling the code, the compiler showed some failed test cases as mentioned below :
```Result Description
test2GetZeroScore:
Please check the logic of getScore method for a valid sentence with incorrect word
test3GetNonZeroScoreOn3Words:
Please check the logic of getScore method for a valid sentence with 3 words
test4GetNonZeroScoreOn3PlusWords:
Please check the logic of getScore method for a valid sentence with 3 plus words
test5ValidateSentence:
Check the logic of the validateSentence method for an invalid sentence
test6ValidateSentence:
Check the logic of the validateSentence method for a valid sentence
testAlliterationUIForScore:
expected:<[yourscoreis2]> but was:<[invalidsentence]>
TEST CASE FAILED```
Please help me solve these errors and suggest corrections in the code.
The code for index.html that I have written is:
Enter the letter
Enter the sentence
“`
The script.js code that I have written is :
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 the number of words in the sentence is <3, display the corresponding output statement in div with id 'result'
if (wordCount < 3) {
document.getElementById('result').innerHTML = "Invalid number of words";
return;
}
// Invoke validateSentence() method to validate the sentence
var isValid = validateSentence(sentence);
// If false, display the corresponding output statement in div with id 'result'
if (!isValid) {
document.getElementById('result').innerHTML = "Invalid sentence";
return;
}
// Invoke getScore() to calculate the score
var score = getScore(sentence, char);
// Display the corresponding output statement in div with id 'result'
document.getElementById('result').innerHTML = 'Your Score is ' + score;
} catch (err) {
document.getElementById('result').innerHTML = "Function checkAlliteration: " + err;
}
}
function getCount(str) {
try {
// Calculates the number of words in str and returns the count
var words = str.match(/bw+b/g);
return words ? words.length : 0;
} catch (err) {
document.getElementById("result").innerHTML = "Function getCount: " + err;
}
}
function validateSentence(str) {
try {
// When any word of str starts with a vowel, return false; else, return true
var words = str.trim().split(/s+/);
var vowels = ['a', 'e', 'i', 'o', 'u'];
for (var i = 0; i < words.length; i++) {
if (vowels.indexOf(words[i][0]) !== -1) {
return false;
}
}
return true;
} catch (err) {
document.getElementById("result").innerHTML = "Function validateSentence: " + err;
}
}
function getScore(str, char) {
try {
// Compare the first letter of every word from str with char, calculate and return the score
var words = str.trim().split(/s+/);
var correctWordCount = 0;
var score = 0;
for (var i = 0; i < words.length; i++) {
if (words[i][0] === char) {
correctWordCount++;
if (correctWordCount <= 3) {
score = 2;
} else {
score += 2;
}
}
}
return score;
} catch (err) {
document.getElementById("result").innerHTML = "Function getScore: " + err;
}
}
});
Please read the test cases that are failed and help me solve these in the script.js file. We dont have to usw JQuery codes. just basic form validation