Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Alexa Intent中循环遍历对象数组?_Javascript_Amazon Web Services_Alexa_Alexa Skills Kit_Alexa Skill_For_Intent - Fatal编程技术网

Javascript 如何在Alexa Intent中循环遍历对象数组?

Javascript 如何在Alexa Intent中循环遍历对象数组?,javascript,amazon-web-services,alexa,alexa-skills-kit,alexa-skill,for,intent,Javascript,Amazon Web Services,Alexa,Alexa Skills Kit,Alexa Skill,For,Intent,所以问题是我无法在quizIntent处理程序的循环中循环通过问题的数组。只显示了第一个问题,然后它不再进一步移动。代码的其余部分工作正常。 我想做的是不断地问问题,直到数组用完,当用户给出错误的答案时,就会跳出循环。 我只是Alexa编程的新手。请帮忙 这是我的数组 const got = [ { question: "Grey Wind, Lady, Ghost, Shaggydog, Summer and the sixth direwolve's name

所以问题是我无法在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意图

    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");
   }

};
循环不起任何作用,因为它被设计为在每次调用时都给出一个响应。因此,尽管您正在运行循环,但它不会发出第二个响应,因为第一次迭代的listen方法将从一开始就开始执行,因为这是一个新的调用

使用会话属性可以成功实现您想要达到的目的

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;

解决这个问题的一个办法是:

    var i = 0;
var handlers = {
  "customIntent": function () {
           this.response.speak("Would you like to take part in a trial by combat?").listen();
      this.emit(":responseReady");
   },
   "quizIntent": function () {
       var mydecision = this.event.request.intent.slots.decision.value;
       if(mydecision=='no'||mydecision=='nope'||mydecision=='naah'){
        this.response.speak("Battles have been won against harder odds! Even little Lyanna Mormont has more courage than you.");
        this.emit(":responseReady");
       }

       if(i<=got.length){
           var item = got[i].question;
           if(i == 0){
                this.response.speak("Be attentive; just like life, I won't repeat or give you a second chance. Here you go; " + item).listen();
                this.emit(":responseReady");
           }
           else {
           this.response.speak(item).listen();
           this.emit(":responseReady");
           }
        }
   },

    "answerIntent": function () {  
            var myanswer = this.event.request.intent.slots.answer.value;

            if(myanswer!=got[i].answer){
                this.response.speak("Wrong Answer. The correct answer is " + got[i].answer + ". You are dead.");
                this.emit(':responseReady');

            }
            i++;
            if(i==got.length){
                i=0;
                this.response.speak("You emerged the ultimate victor. The best in all of Planetos.");
                this.emit(':responseReady');
            }
            this.response.speak("You survived. Say ready, when you are, for the next combat!").listen();
            this.emit(':responseReady');
    },
var i=0;
变量处理程序={
“customIntent”:函数(){
this.response.speak(“你想参加战斗审判吗?”).listen();
这个.emit(“:responseReady”);
},
“quizIntent”:函数(){
var mydecision=this.event.request.intent.slots.decision.value;
如果(mydecision=='no'| | mydecision=='nope'| | mydecision=='naah'){
这个。回应。说话(“在更困难的情况下赢得了战斗!即使是小Lyanna Mormont也比你更有勇气。”);
这个.emit(“:responseReady”);
}

如果(iIs)这是AWS lambda技能?是。问题在于lambda函数尝试并增加lambda函数的超时时间。由于对话是异步调用,可能lambda函数超时。
    var i = 0;
var handlers = {
  "customIntent": function () {
           this.response.speak("Would you like to take part in a trial by combat?").listen();
      this.emit(":responseReady");
   },
   "quizIntent": function () {
       var mydecision = this.event.request.intent.slots.decision.value;
       if(mydecision=='no'||mydecision=='nope'||mydecision=='naah'){
        this.response.speak("Battles have been won against harder odds! Even little Lyanna Mormont has more courage than you.");
        this.emit(":responseReady");
       }

       if(i<=got.length){
           var item = got[i].question;
           if(i == 0){
                this.response.speak("Be attentive; just like life, I won't repeat or give you a second chance. Here you go; " + item).listen();
                this.emit(":responseReady");
           }
           else {
           this.response.speak(item).listen();
           this.emit(":responseReady");
           }
        }
   },

    "answerIntent": function () {  
            var myanswer = this.event.request.intent.slots.answer.value;

            if(myanswer!=got[i].answer){
                this.response.speak("Wrong Answer. The correct answer is " + got[i].answer + ". You are dead.");
                this.emit(':responseReady');

            }
            i++;
            if(i==got.length){
                i=0;
                this.response.speak("You emerged the ultimate victor. The best in all of Planetos.");
                this.emit(':responseReady');
            }
            this.response.speak("You survived. Say ready, when you are, for the next combat!").listen();
            this.emit(':responseReady');
    },