gpt4 book ai didi

javascript - 如何循环遍历 Alexa Intent 中的对象数组?

转载 作者:行者123 更新时间:2023-11-30 20:59:03 25 4
gpt4 key购买 nike

所以问题是我无法在 quizIntent 处理程序的循环中遍历问题数组。仅显示第一个问题,然后不再移动。其余代码工作正常。我想做的是继续提问,直到数组中的问题用完,并在用户给出错误答案时跳出循环。

我只是 Alexa 编程的新手。请帮忙。

这是我的数组

    const got = [
{
question: "Grey Wind, Lady, Ghost, Shaggydog, Summer and the sixth direwolve's name is?",
answer: 'nymeria',
},
{
question: "What was the name of the sinister castle where Arya and Gendry were held prisoner in season two?",
answer: 'harrenhal',
},
{
question: "What is a person called that can enter the minds of animals?",
answer: 'warg',
},
{
question: "What was the name of the Stark ancestral sword that was melted down by Tywin Lannister?",
answer: 'ice',
}
];

这是我的 Alexa Intent

    var handlers = {
"customIntent": function () {
this.response.speak("Would you like to appear for a trial by combat");
this.emit(":responseReady");
},
"quizIntent": function () {
var mydecision = this.event.request.intent.slots.decision.value;
if(mydecision=='no'||mydecision=='nope'||mydecision=='naah'){
this.response.speak("A five year old has more courage than you.");
this.emit(":responseReady");
}

for(var i = 0; i <got.length; i++){
var myanswer = this.event.request.intent.slots.answer.value;
var item = got[i].question;
this.response.speak(item).listen();
if(myanswer!=got[i].answer){
this.response.speak("Wrong Answer. You are dead");
this.emit(':responseReady');

}
if(i==got.length-1){
this.response.speak("You won");
this.emit(':responseReady');
}

}


},
"LaunchRequest": function () {
this.response.speak("Valar Morghulis").listen("You are supposed to say Valar Dohareis");
this.emit(":responseReady");
}

};

如果你也想看的话,这是我的 Intent Schema

{
"languageModel": {
"types": [
{
"name": "answerSlot",
"values": [
{
"id": null,
"name": {
"value": "Nymeria",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Harrenhal",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Warg",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Ice",
"synonyms": []
}
}
]
},
{
"name": "decisionSlot",
"values": [
{
"id": null,
"name": {
"value": "Yes",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "No",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Naah",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Yeah",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Yup",
"synonyms": []
}
},
{
"id": null,
"name": {
"value": "Nope",
"synonyms": []
}
}
]
}
],
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "customIntent",
"samples": [
"Valar Dohareis",
"hello",
"hola"
],
"slots": []
},
{
"name": "quizIntent",
"samples": [
"{decision}",
"The answer is {answer}"
],
"slots": [
{
"name": "decision",
"type": "decisionSlot"
},
{
"name": "answer",
"type": "answerSlot"
}
]
}
],
"invocationName": "quiz game"
}
}

最佳答案

循环没有任何作用 旨在对每次调用给出单一响应。所以虽然你是循环将运行,它不会发出第二个响应,因为在第一次迭代的 listen 方法之后执行将从一开始就开始,因为它是一个新的调用。

使用Session attribute可以顺利达到你想要达到的目的。

var iterations = 0; //defined globally

// Later on, for every iteration, you simply need to call
// into the attributes property of the alexa object to change the value.

this.attributes['iterations'] = iterations + 1;

关于javascript - 如何循环遍历 Alexa Intent 中的对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47309999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com