FAQ: Session Attributes - Create the Lambda Function

This community-built FAQ covers the “Create the Lambda Function” exercise from the lesson “Session Attributes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Introduction to Alexa

FAQs on the exercise Create the Lambda Function

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

The code given in the lesson does not work. I am not terribly familiar with all of this, but I wrote the similar program that does work. I tried to use the same good coding practice shown in the example.

I used the fact skill template, and had slots
Animals: cats, birds, and dogs,
Answers: 4, 2, small, both, bark, meow, and chirp

Here is my code below

/* eslint-disable func-names /
/
eslint-disable no-console */

const Alexa = require(‘ask-sdk’);
var globalAnimals;
var cardsTaken = 0;
var points = 0;
var flashcards = [
{
question: “make what sound?”,
dogsAnswer: “bark”,
catsAnswer: “meow”,
birdsAnswer: “chirp”,
},
{
question: “have how many legs?”,
dogsAnswer: “4”,
catsAnswer: “4”,
birdsAnswer: “2”,
},
{
question: " are generally small, large, or both?",
dogsAnswer: “both”,
catsAnswer: “small”,
birdsAnswer: “small”,
},
];

const GetNewFactHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === ‘LaunchRequest’
|| (request.type === ‘IntentRequest’
&& request.intent.name === ‘GetNewFactIntent’);
},
handle(handlerInput) {

return handlerInput.responseBuilder
  .speak('Welcome to flashcards! What animal will you choose?')
  .reprompt('come again?')
  .getResponse();

},
};

const SetMyAnimalIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === ‘IntentRequest’
&& request.intent.name === ‘SetMyAnimalIntent’;
},
handle(handlerInput) {
var animal = handlerInput.requestEnvelope.request.intent.slots.animals.value;
globalAnimals = animal;

return handlerInput.responseBuilder
  .speak("You have chosen " + animal + ". Are you ready for your first question?")
  .reprompt("is that okay?")
  .getResponse();

},

};

const FirstQuestionIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === ‘IntentRequest’
&& request.intent.name === ‘FirstQuestionIntent’;
},
handle(handlerInput) {

const speakOutput = globalAnimals + " " + flashcards[cardsTaken].question;

return handlerInput.responseBuilder
  .speak(speakOutput)
  .reprompt("say that again")
  .getResponse();

},

};

const AnswersIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === ‘IntentRequest’
&& request.intent.name === ‘AnswersIntent’;
},
handle(handlerInput) {
var answer = handlerInput.requestEnvelope.request.intent.slots.answers.value;
var correctAnswer = globalAnimals + ‘Answer’;
var finalAnswer = flashcards[cardsTaken][correctAnswer];

var speakOutput


if(answer === finalAnswer)
{
  speakOutput = "That is correct! "
  points++;
}
else
{
  speakOutput = "That is wrong! You answered " + answer + ". The right answer is " + finalAnswer + ". ";
}

cardsTaken++;

if(cardsTaken < flashcards.length)
{
  speakOutput+= " Are you ready for the next question?"

return handlerInput.responseBuilder
  .speak(speakOutput)
  .reprompt("say that again")
  .getResponse();
}
else
{
  speakOutput += "That was the final flashcard. You got " + points + " out of " +   cardsTaken + " possible.";
  
  return handlerInput.responseBuilder
  .speak(speakOutput)
  .getResponse();
}

},

};

const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === ‘IntentRequest’
&& request.intent.name === ‘AMAZON.HelpIntent’;
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(HELP_MESSAGE)
.reprompt(HELP_REPROMPT)
.getResponse();
},
};

const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === ‘IntentRequest’
&& (request.intent.name === ‘AMAZON.CancelIntent’
|| request.intent.name === ‘AMAZON.StopIntent’);
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(STOP_MESSAGE)
.getResponse();
},
};

const SessionEndedRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === ‘SessionEndedRequest’;
},
handle(handlerInput) {
console.log(Session ended with reason: ${handlerInput.requestEnvelope.request.reason});

return handlerInput.responseBuilder.getResponse();

},
};

const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(Error handled: ${error.message});

return handlerInput.responseBuilder
  .speak('Sorry, an error occurred.')
  .getResponse();

},
};

const SKILL_NAME = ‘Animal Facts’;
const GET_FACT_MESSAGE = 'Here’s your fact: ';
const HELP_MESSAGE = ‘You can say tell me a space fact, or, you can say exit… What can I help you with?’;
const HELP_REPROMPT = ‘What can I help you with?’;
const STOP_MESSAGE = ‘Goodbye!’;

const skillBuilder = Alexa.SkillBuilders.standard();

exports.handler = skillBuilder
.addRequestHandlers(
GetNewFactHandler,
SetMyAnimalIntentHandler,
FirstQuestionIntentHandler,
AnswersIntentHandler,
HelpHandler,
ExitHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();