question

Ashish Jha avatar image
Ashish Jha asked

How to loop through a object array in an Alexa Intent?

So the problem is I am not able to loop through the question's array in the loop in the quizIntent handler. Only the first question is shown and then it does not move any further. Rest of the code is working fine. What I want to do is Keep on asking questions till the array runs out of them and break out of the loop when the user gives a wrong answer.

I am just a newbie to Alexa Programming. Please help.

Here is my array

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

Here is my Alexa Intent

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

};

Here is my Intent Schema in case you wanna look at that too

{
  "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"
  }
}
alexa skills kitlambdaintentsalexa skills challengeschema
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers