-3

Getting a console error as the title says on this portion of code:

btn.addEventListener('click', () => {
    recognition.start();
});

This is the JS

    const btn = document.querySelector('.talk');
const content = document.querySelector('.txt-content');

// Custom response array for Greetings
const greetings = ['I\'m doing well, thanks', 'Doing good brother!', 'Please leave me alone, not in the mood brah'];

// Custom response array for Weather 
const weather = ['The weather is fine today', 'You need a tan good sir', 'It is going to rain cats and dogs'];

const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();

recognition.onstart = function () {
    console.log('voice is activated, you can speak into your microphone');
};

recognition.onspeechend = function (event) {
    const current = event.resultIndex;
    const transcript = event.results[current][0].transcript;
    content.textContent = transcript;
    readOutLoud(transcript);
};

// Add the listener to the button
btn.addEventListener('click', () => {
    recognition.start();
});

function readOutLoud(message) {
    const speech = new SpeechSynthesisUtterance();

    speech.text = 'i dont know what you said';

    // if statement for custom response array
    if (message.includes('how are you')) {
        const finalText = greetings[Math.floor(Math.random() * greetings.length)];
        speech.text = finalText;
    }

    speech.volume = 1;
    speech.rate = 1;
    speech.pitch = 1;

    window.speechSynthesis.speak(speech);
}
  • 3
    Because `btn` is null. We will have no idea why unless you show us how your are acquiring `btn` in the first place (JavaScript and HTML). – Get Off My Lawn Jul 08 '19 at 18:04

1 Answers1

-2

This is because, btn is pointing to null. Please check, what do you get when your console btn.

Chetan Gawai
  • 2,133
  • 18
  • 34