我编码我自己的贾维斯(排序的,没有什么更接近铁人之一) 但其他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"> (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);
}
我很惊讶它得到了“早上好”,因为你必须在首都... –
你尝试登录'varpo'安慰? – CuriousMind
它说*“读你的F.B.通知。”*如果我通过''你好''。你必须传递别的东西。尝试'var varpo = code.trim();' – Phil