// alexa-cookbook sample code

// There are three sections, Text Strings, Skill Code, and Helper Function(s).
// You can copy and paste the entire file contents as the code for a new Lambda function,
//  or copy & paste section #3, the helper function, to the bottom of your existing Lambda code.


// 1. Text strings =====================================================================================================
//    Modify these strings and messages to change the behavior of your Lambda function

const myRequest = ['apples', 'oranges', 'strawberries'];  // Array of items

// 2. Skill Code =======================================================================================================


const Alexa = require('alexa-sdk');

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);

    // alexa.appId = 'amzn1.echo-sdk-ams.app.1234';
    // alexa.dynamoDBTableName = 'YourTableName'; // creates new table for session.attributes

    alexa.registerHandlers(handlers);
    alexa.execute();
};

const handlers = {
    'LaunchRequest': function () {
        this.emit('MyIntent');
    },

    'MyIntent': function () {

        var speechOutput = 'the list contains ' + sayArray(myRequest,  'and')
                    + '. please choose either ' +  sayArray(myRequest,  'or');
        this.response.speak(speechOutput);
        this.emit(':responseReady');
    }
};


//    END of Intent Handlers {} ========================================================================================
// 3. Helper Function  =================================================================================================


function sayArray(myData, andor) {
    // the first argument is an array [] of items
    // the second argument is the list penultimate word; and/or/nor etc.

    var listString = '';

    if (myData.length == 1) {
        listString = myData[0];
    } else {
        if (myData.length == 2) {
            listString = myData[0] + ' ' + andor + ' ' + myData[1];
        } else {

            for (var i = 0; i < myData.length; i++) {
                if (i < myData.length - 2) {
                    listString = listString + myData[i] + ', ';
                    if (i = myData.length - 2) {
                        listString = listString + myData[i] + ', ' + andor + ' ';
                    }

                } else {
                    listString = listString + myData[i];
                }

            }
        }

    }

    return(listString);
}

@@ -555,16 +555,38 @@
 berries'
+, 'lemons', 'cherries'
 %5D;  // A
@@ -1205,19 +1205,19 @@
         
-var
+let
  speechO
@@ -1715,21 +1715,39 @@
 myData, 
-andor
+penultimateWord = 'and'
 ) %7B%0A    
@@ -1868,281 +1868,96 @@
 etc.
-%0A%0A    var listString = '';%0A%0A    if (myData.length == 1) %7B%0A        listString = myData%5B0%5D;%0A    %7D else %7B%0A        if (myData.length == 2) %7B%0A            listString = myData%5B0%5D + ' ' + andor + ' ' + myData%5B1%5D;%0A        %7D else %7B%0A%0A            for (var i = 0; i %3C myData.length; i++
+  Default to 'and'%0A    let result = '';%0A%0A    myData.forEach(function(element, index, arr
 ) %7B%0A
@@ -1956,32 +1956,33 @@
  arr) %7B%0A        
+%0A
         if (i %3C 
@@ -1982,122 +1982,79 @@
 f (i
- %3C myData.length - 2) %7B%0A                    listString = listString + myData%5Bi%5D + ', ';%0A                    if (i 
+ndex === 0) %7B%0A            result = element;%0A        %7D else if (index ==
 = my
@@ -2067,17 +2067,17 @@
 ength - 
-2
+1
 ) %7B%0A    
@@ -2088,206 +2088,107 @@
     
-            listString = listString + myData%5Bi%5D + ', ' + andor + ' ';%0A                    %7D%0A%0A                %7D else %7B%0A                    listString = listString + myData%5Bi%5D;%0A                %7D%0A%0A    
+result += %60 $%7BpenultimateWord%7D $%7Belement%7D%60;%0A        %7D else %7B%0A            result += %60, $%7Belement%7D%60;%0A
     
@@ -2201,21 +2201,11 @@
     
-   
- %7D%0A%0A    %7D%0A
+%7D);
 %0A   
@@ -2215,20 +2215,14 @@
 turn
-(listString)
+ result
 ;%0A%7D
-%0A

Fix sayArray bug which caused items to be skipped