Guest User

Untitled

a guest
Jan 28th, 2018
159
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. var handlers = {
  28.   // Open Codecademy Flashcards
  29.   'LaunchRequest': function() {
  30.     this.attributes['language'] = '';
  31.     this.attributes['numberCorrect'] = 0;
  32.     this.attributes['currentFlashcardIndex'] = 0;
  33.  
  34.     this.response
  35.         .speak('Welcome to Flashcards. Do you want to test' +
  36.         ' your knowledge in Ruby, Python, or Javascript? Say Practice followed by the language.').listen(
  37.         'Which language would you like to practice?');
  38.     this.emit(':responseReady');
  39.   },
  40.  
  41.   'SetMyLanguageIntent': function() {
  42.     this.attributes.flashcards.currentLanguage = this.event.request.intent.slots.languages.value;
  43.     var currentLanguage = this.attributes.flashcards.currentLanguage;
  44.  
  45.     this.response
  46.         .speak('Okay, I will ask you some questions about ' +
  47.         currentLanguage + '. Here is your first question.' +
  48.                 AskQuestion(this.attributes)).listen(AskQuestion(this.attributes));
  49.     this.emit(':responseReady');
  50.   },
  51.  
  52.   // User gives an answer
  53.   'AnswerIntent': function() {
  54.     var userAnswer = this.event.request.intent.slots.answer.value;
  55.     var language = this.attributes['language'];
  56.     var languageAnswer = language + 'Answer';
  57.     var correctAnswer = flashcardsDictionary[this.attributes['currentFlashcardIndex']][languageAnswer];
  58.  
  59.  
  60.     if (userAnswer === correctAnswer) {
  61.       this.attributes['numberCorrect']++;
  62.       var numberCorrect = this.attributes['numberCorrect'];
  63.  
  64.       this.response
  65.           .speak('Nice job! The correct answer is ' + correctAnswer + '. You ' +
  66.             'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' ' +
  67.             language + ' questions correct.' + this.AskQuestion)
  68.           .listen(this.AskQuestion);
  69.  
  70.  
  71.     } else {
  72.       var numberCorrect = this.attributes['numberCorrect'];
  73.  
  74.       this.response
  75.           .speak('Sorry, the correct answer is ' + correctAnswer + '. You ' +
  76.           'have gotten ' + numberCorrect + ' out of ' + DECK_LENGTH + ' ' +
  77.           language + ' questions correct. Here is your next question.' +
  78.                  this.AskQuestion).listen(this.AskQuestion);
  79.     }
  80.  
  81.     this.attributes['currentFlashcardIndex']++;
  82.  
  83.     this.emit(':responseReady');
  84.   },
  85.  
  86.   // Stop
  87.   'AMAZON.StopIntent': function() {
  88.       this.response.speak('Ok, let\'s play again soon.');
  89.       this.emit(':responseReady');
  90.   },
  91.  
  92.   // Cancel
  93.   'AMAZON.CancelIntent': function() {
  94.       this.response.speak('Ok, let\'s play again soon.');
  95.       this.emit(':responseReady');
  96.   }
  97. };
  98.  
  99.    // Test my {language} knowledge
  100. function AskQuestion(attributes) {
  101.     //return "hello" + attributes.flashcards.currentLanguage;
  102.     var currentLanguage = attributes.flashcards.currentLanguage;
  103.     var currentFlashcardIndex = attributes.flashcards.languages[currentLanguage].currentFlashcardIndex;
  104.     var currentQuestion = flashcardsDictionary[currentFlashcardIndex].question;
  105.     var formedQues = 'In ' + currentLanguage +', ' + currentQuestion;
  106.     return formedQues;
  107. }
  108.  
  109. exports.handler = function(event, context, callback){
  110.     var alexa = Alexa.handler(event, context);
  111.     alexa.registerHandlers(handlers);
  112.     alexa.execute();
  113. };
RAW Paste Data