0

問題は、quizIntentハンドラのループ内で質問の配列をループすることができないことです。最初の質問のみが表示され、それ以上は移動しません。残りのコードは正常に動作しています。 私がしたいことはです。ユーザーが間違った答えを出すと、配列が使い果たされてループから抜け出すまで、質問を続けてください。Alexa Intentでオブジェクト配列をループする方法は?

私はAlexa Programmingのちょっとした初心者です。助けてください。ここで

は私の配列ここ

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', 
    } 
]; 

はここに私のアレクサ・インテント

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

}; 

は私の意図スキーマは、あなたがdoesnの場合には、あまりにも

{ 
    "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" 
    } 
} 
+0

これはAWSラムダスキルですか? – MangoBoy

+0

はい。問題はラムダ関数の場合です – thedreamsaver

+0

ラムダ関数のタイムアウトを試してください。 Speakingは非同期呼び出しであるため、ラムダ関数がタイムアウトしている可能性があります。 – MangoBoy

答えて

0

一つの方法は:

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'); 
    }, 

アイデアはクイズの意図と解答の意図を前後に切り替えることです。

1

ループでたく外観ですですはどんな目的にも役立ちません。単一呼び出し。したがって、あなたはループですが、新しい呼び出しであるため、最初の反復のlistenメソッドが最初から開始された後、2番目の応答は実行されません。

セッション属性を使用すると、達成したい目的を達成できます。私はこの問題を解決するために考え出した

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; 
関連する問題