简体   繁体   English

为什么第二个if语句在我的代码中不起作用?

[英]Why isn't my second if statement working in my code?

I'm currently working on a project where if you click a button and say something the computer will respond. 我目前正在研究一个项目,如果您单击按钮并说出计算机会响应的内容。 I have set up a const called greetings and inside lays some strings I've put. 我已经建立了一个名为“问候”的常量,并在其中放置了一些我放入的字符串。 I then put an if statement in my readOutLoud function. 然后,我在readOutLoud函数中放置一个if语句。 In the if statement the computer checks if the user says any of the keywords (right now it can only respond to how are you) and then uses Math. 在if语句中,计算机检查用户是否输入了任何关键字(现在它只能响应您的情况),然后使用Math。 Floor random which I've also inserted in the if statement to pick one of the 3 choices I've put in the greetings const. 随机下限,我也插入了if语句,以选择我在问候语const中输入的3个选项之一。 So right now if you press the talk button and say how are you it will respond with one of the 3 choices I've put in the greetings const. 因此,如果您现在按下通话按钮并说出您的状况,它将以我在问候常量中输入的3个选项之一做出响应。 For some reason the second if statement I've put below the 1st one doesn't work why is that so? 由于某种原因,我放在第一条下面的第二条if语句不起作用,为什么会这样呢? The only thing different about the second one is that it's using different keywords and getting another const. 第二个唯一的不同是它使用了不同的关键字并获得了另一个const。

 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('hey', 'hi', 'hello')) { const finalText = hello[Math.floor(Math.random() * hello.length)]; speech.text = finalText; } speech.volume = 1; speech.rate = 1; speech.pitch = 1; window.speechSynthesis.speak(speech); } 
 <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <button class="talk">Talk</button> <h3 class="content"></h3> </body> 

Your code doesn't work because the "includes" method checks whether an array contains a certain value, not 1 of many values. 您的代码无效,因为“ includes”方法检查数组是否包含某个值,而不是多个值中的1个。 You can make this work by using the "some" method like so. 您可以使用“ some”方法来完成这项工作。

if(['hey', 'hi', 'hello'].some(word => message.includes(word))) {
    const finalText = hello[Math.floor(Math.random() * hello.length)];
    speech.text = finalText;
}

This will iterate through the array, checking if the message includes any of the specified values. 这将遍历数组,检查消息是否包含任何指定值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM