Optimize JavaScript Code and to create correct cases for failed TEST CASES

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:

  1. On creating a sentence exactly with 3 correct words (or) for the first 3 correct words of the sentence, the score is 2.
  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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật