mardi 18 juin 2019

How should I set up my if statements to make this code work?

I am making a web app which just answers some questions stuff. I've set up a const called greeting and inside the const lays an array which has some random greetings I've put. I then added an if statement with Math.Floor so the computer can choose a random greeting to say, this works just fine I ask the computer how are you and responds with one of the 3 options I've put. I've made several other consts and I want to do the same thing I did with the other one but when I put the if statement it doesn't work. I also have another question if I only put one option for the computer in const then I obviously don't need Math.Floor random in the if statement I would need to choose that specific greeting I put in the const so how would I do that? I truly appreciate anyone who at least tries to help me because I know I'm very bad at explaining, make sure to look at the code. Oh and make sure to run the code snippet in a google chrome browser because I don't think it works anywhere else even on StackOverflow

<!DOCTYPE html>
<html>
<head>
        <title>Page Title</title>
</head>
<body>
<button class="talk">Talk</button>
<h3 class="content"></h3>


</body>
<script>
const btn = document.querySelector('.talk');
const content = document.querySelector('.content');
const greetings = [
                                'If you are good im good to 😉', 
                                'Im doin alright', 
                                'Im tired 😮'
];
const weather = [
                                'Ask the weatherman!', 
                                'okay I guess' 
                                
];
const name = [
        'My name is techwali', 
        'techwali!'
];
const hello = [
        'Why hello! How are you doing today?', 
        'Hey there How are you?'
]
const hru = [
        'thats great!', 
        'Im so sorry to hear that', 
        'Feel better soon!'
]
const SpeechRecognition = 
        window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();

recognition.onstart = function() {
        console.log('voice is activated speak into the mic');
};
recognition.onresult = function(event) {
        const current = event.resultIndex;
        
        const transcript = event.results[current][0].transcript;
        content.textContent = transcript;
        readOutLoud(transcript);
}

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

function readOutLoud(message) {
        
        const speech = new SpeechSynthesisUtterance();
        speech.text = 'I dont know what you said';
        if(message.includes('how are you')) {
                 const finalText = 
                 greetings[Math.floor(Math.random() * greetings.length)];
                 speech.text = finalText;
                
        } if(message.includes('whats your name', 'your name')) {
                 const finalText = 
                 name[Math.floor(Math.random() * name.length)];
                 speech.text = finalText;
                
        }
        
        speech.volume = 1;
        speech.rate = 1;
        speech.pitch = 1;
        window.speechSynthesis.speak(speech);
        
        
}
</script>
</html>

.

Aucun commentaire:

Enregistrer un commentaire