2015-08-21 21 views
-1

我编码我自己的贾维斯(排序的,没有什么更接近铁人之一) 但其他if语句不工作如果,否则,如果不工作的JavaScript

function ttalk(code) { 
    varpo = code; 
    if (varpo == "good morning") { 
     var currentdate = new Date(); 
     var hours = currentdate.getHours(); 
     if (hours >= 12) { 
      dn=PM; 
     } else { 
      dn=AM; 
     } 
     varso = "Good Morning, Sir. The time now is " + currentdate.getHours() + " " + currentdate.getMinutes() + dn; 
    } else if (varpo == "hello") { 
     varso = "reading your F.B. Notifications."; 
    } else { 
     varso = "Not Got That, Sir"; 
    } 

    speaker.speak("en", varso); 
} 

当我说Good Morning它发言time now,但每当我说hello它说Not Got That, Sir,为什么?

与输入场测试的完整代码试试这个

Enter Text To Play: 
<input id="text">&nbsp;(Recognized text for voice will also appear here) 
<br><br> 
<button class="btn btn-success" onclick="stalk()">Talk It!</button> 
<button class="btn btn-success" onclick="listen()">Recognize My Voice</button> 

<script> 
    var speaker = new RobotSpeaker(); 
    var listener = new AudioListener(); 

function stalk() { 
var varpo = document.getElementById("text").value; 
if (varpo == "good morning") { 
    var currentdate = new Date(); 
    var hours = currentdate.getHours(); 
      if (hours >= 12) { dn=PM;} else {dn=AM;} 
     varso = "Good Morning, Sir. The time now is " + currentdate.getHours() + " " + currentdate.getMinutes() + dn; 
} 
else if (varpo == "hello") { 
    varso = "reading your F.B. Notifications."; 
} 
else { 
    varso = "Not Got That, Sir"; 
} 
     speaker.speak("en", varso); 
    } 
+4

我很惊讶它得到了“早上好”,因为你必须在首都... –

+0

你尝试登录'varpo'安慰? – CuriousMind

+1

它说*“读你的F.B.通知。”*如果我通过''你好''。你必须传递别的东西。尝试'var varpo = code.trim();' – Phil

回答

0

请试试这个,使用的JavaScript的报价内部字符串比较。

function ttalk(code) { 
    varpo = code; 
    if (varpo.localeCompare("good morning")==0) { 
     var currentdate = new Date(); 
     var hours = currentdate.getHours(); 
     if (hours >= 12) { 
      dn=PM; 
     } else { 
      dn=AM; 
     } 
     varso = "Good Morning, Sir. The time now is " + currentdate.getHours() + " " + currentdate.getMinutes() + dn; 
    } else if (varpo.localeCompare("hello")==0) { 
     varso = "reading your F.B. Notifications."; 
    } else { 
     varso = "Not Got That, Sir"; 
    } 

    speaker.speak("en", varso); 
} 

感谢 阿米特

1

我想你的代码和它的工作的罚款只是你需要改变PM和AM分别为“PM”和“AM”。

当我用“早上好”执行时,输出如下:

早上好,先生。现在的时间是14点58分

当你通过“早上好”时,它显示如下。由于区分大小写。

没有了,先生

+1

问题是由'google speak recognition API'生成的字符串..如果您手动输入字符串,它肯定会起作用 –

+0

Yupp @DyrandzFamador这就是我想说的问题既不是案件也不是修剪。 – IdidntKnewIt

+0

@IdidntKnewIt,你是否试图检查变量'code'的值来检查实际的字符串值? –