Guest User

Code academy alexa - sessions

a guest
Jan 29th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var Alexa = require('alexa-sdk');
  4.  
  5. var flashcardsDictionary = [
  6.     {
  7.       question: 'how do you find the length of a string?',
  8.       rubyAnswer: 'length',
  9.       pythonAnswer: 'Len',
  10.       javascriptAnswer: 'length'
  11.     },
  12.     {
  13.       question: 'how do you print to the console or terminal?',
  14.       rubyAnswer: 'puts',
  15.       pythonAnswer: 'print',
  16.       javascriptAnswer:'console.log'
  17.     },
  18.     {
  19.        question:'are boolean terms capitalized or not capitalized?',
  20.        rubyAnswer: 'not capitalized',
  21.        pythonAnswer: 'capitalized',
  22.        javascriptAnswer: 'not capitalized'
  23.      }];
  24.  
  25. var DECK_LENGTH = flashcardsDictionary.length;
  26.  
  27.  
  28. // Test my {language} knowledge
  29.   function AskQuestion(attributes) {
  30.     //return "hello" + attributes.flashcards.currentLanguage;
  31.     var currentLanguage = attributes.flashcards.currentLanguage;
  32.     var currentFlashcardIndex = attributes.flashcards.languages[currentLanguage].currentFlashcardIndex;
  33.     var currentQuestion = flashcardsDictionary[currentFlashcardIndex].question;
  34.     var formedQues = 'In ' + currentLanguage +', ' + currentQuestion;
  35.     return formedQues;
  36.   }
  37.  
  38. var handlers = {
  39.   // Open Codecademy Flashcards
  40.   'LaunchRequest': function() {
  41.     this.attributes['language'] = '';
  42.     this.attributes['numberCorrect'] = 0;
  43.     this.attributes['currentFlashcardIndex'] = 0;
  44.  
  45.     this.response
  46.         .listen('Welcome to Flashcards. In this session, do you want to test' +
  47.         ' your knowledge in Ruby, Python, or Javascript?').speak(
  48.         'Which language would you like to practice?');
  49.     this.emit(':responseReady');
  50.   },
  51.  
  52.   'SetMyLanguageIntent': function() {
  53.     this.attributes['language'] = this.event.request.intent.slots.languages.value;
  54.     var language = this.attributes['language'];
  55.        
  56.     this.response
  57.         .speak('Okay, I will ask you some questions about ' +
  58.         language + '. Here is your first question.' +
  59.                 AskQuestion(this.attributes)).listen(AskQuestion(this.attributes));
  60.     this.emit(':responseReady');
  61.   },
  62.  
  63.   // User gives an answer
  64.   'AnswerIntent': function() {
  65.     var userAnswer = this.event.request.intent.slots.answer.value;
  66.    
  67.     var language = this.attributes['language'];
  68.     var languageAnswer = language + 'Answer';
  69.  
  70.     var correctAnswer = flashcardsDictionary[this.attributes['currentFlashcardIndex']][languageAnswer];
  71.  
  72.     if (userAnswer === correctAnswer) {
  73.      
  74.       this.attributes['numberCorrect']++;
  75.       var numberCorrect = this.attributes['numberCorrect'];
  76.      
  77.       this.response
  78.           .speak('Nice job! The correct answer is ' + correctAnswer + '. You ' +
  79.             'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' ' +
  80.             language + ' questions correct.' + AskQuestion(this.attributes))
  81.           .listen(AskQuestion(this.attributes));
  82.  
  83.  
  84.     } else {
  85.      
  86.       var numberCorrect = this.attributes['numberCorrect'];
  87.       this.response
  88.           .speak('Sorry, the correct answer is ' + correctAnswer + '. You ' +
  89.           'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' ' +
  90.           language + ' questions correct. Here is your next question.' +
  91.                  AskQuestion(this.attributes)).listen(AskQuestion(this.attributes));
  92.     }
  93.  
  94.     this.attributes['currentFlashcardIndex']++;
  95.     this.emit(':responseReady');
  96.   },
  97.  
  98.   // Stop
  99.   'AMAZON.StopIntent': function() {
  100.       this.response.speak('Ok, let\'s play again soon.');
  101.       this.emit(':responseReady');
  102.   },
  103.  
  104.   // Cancel
  105.   'AMAZON.CancelIntent': function() {
  106.       this.response.speak('Ok, let\'s play again soon.');
  107.       this.emit(':responseReady');
  108.   }
  109. };
  110.  
  111. exports.handler = function(event, context, callback){
  112.     var alexa = Alexa.handler(event, context);
  113.     alexa.registerHandlers(handlers);
  114.     alexa.execute();
  115. };
Add Comment
Please, Sign In to add comment