My skill: Making a juice suggestion where when the user says a state, Alexa will give a juice suggestion based off of that state. The state and juice pair is stored into an array.
Challenge: Currently, my skill will only execute once. Is there a way where I can have a loop where after Alexa gives a suggestion, Alexa will ask the question again and wait for the user response? Attached is the code I have with my skill. Thanks.
var Alexa = require('alexa-sdk');
const APP_ID = undefined;
const skillData = [
{
state: "FLORIDA",
suggestion: "My suggestion for Florida is organic orange juice by Naked"
},
{
state: "CALIFORNIA",
suggestion: "My suggestion for California is pomegrante by POM!!"
},
{
state: "NEW JERSEY",
suggestion: "My suggestion for Jersey is blueberry by Jersey Fresh"
}
];
var number = 0;
while(number<3){
var handlers = {
'LaunchRequest': function () {
this.emit(':ask', 'I can suggest a juice from any state in the United States. What state would you like a juice suggestion for?', 'Tell me a state name and I will suggest a local juice from there.');
},
'MakeSuggestion': function() {
var stateSlot = this.event.request.intent.slots.state.value;
this.emit(':tell', getSuggestion(skillData, 'state', stateSlot.toUpperCase()).suggestion);
},
'Unhandled': function () {
this.emit(':tell', 'Sorry, I don\'t know what to do');
},
'AMAZON.HelpIntent': function () {
this.emit(':ask', "What can I help you with?", "How can I help?");
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', "Okay!");
},
'AMAZON.StopIntent': function () {
this.emit(':tell', "Goodbye!");
},
}
number = number+1;
};
exports.handler = function(event, context){
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
function getSuggestion(arr, propName, stateName) {
for (var i=0; i < arr.length; i++) {
if (arr[i][propName] == stateName) {
return arr[i];
}
}
}