0

I hope someone can help me. I try to create a multilingual Alexa skill.

There are language constants in my main file like provided from the sample code from Alexa like this:

const languageString = {
'en': {
    'translation': {
        'QUESTIONS': questions['QUESTIONS_EN_US'],
        'GAME_NAME': 'My Quiz', // Be sure to change this for your skill.
        'HELP_MESSAGE': 'I will ask you %s multiple choice questions. Respond with the number of the answer. ' +
            'For example, say one, two, three, or four. To start a new game at any time, say, start game. ',

Everything is working fine and so I wanted to add now some alternate strings, so that the game is less boring. So I created a correct.js file and defined it in my document like this:

const correct = require('./correct');

This file looks like this:

'use strict';

module.exports = {

    CORRECT_EN_GB: [
        "Booya", "All righty", "Bam", "Bazinga", "Bingo", "Boom", "Bravo", "Cha Ching", "Cheers", "Dynomite",
        "Hip hip hooray", "Hurrah", "Hurray", "Huzzah", "Oh dear.  Just kidding.  Hurray", "Kaboom", "Kaching", "Oh snap", "Phew",
        "Righto", "Way to go", "Well done", "Whee", "Woo hoo", "Yay", "Wowza", "Yowsa"
    ],
    CORRECT_EN_US: [
        "Booya", "All righty", "Bam", "Bazinga", "Bingo", "Boom", "Bravo", "Cha Ching", "Cheers", "Dynomite",
        "Hip hip hooray", "Hurrah", "Hurray", "Huzzah", "Oh dear.  Just kidding.  Hurray", "Kaboom", "Kaching", "Oh snap", "Phew",
        "Righto", "Way to go", "Well done", "Whee", "Woo hoo", "Yay", "Wowza", "Yowsa"
    ],
    CORRECT_DE_DE: [
        "Aber hallo", "Bazinga", "Bingo", "Bravo", "Donnerwetter",
        "en garde", "hipp hipp hurra", "hurra", "japp", "jawohl", "jo", "juhu", "na sieh mal einer an", "Stimmt",
        "Super", "Supi", "tada", "türlich", "yay"
    ],
};

Now i want to retrieve a random value of the list inside my language strings and tried this code:

            'ANSWER_CORRECT_MESSAGE': '<say-as interpret-as="interjection">' + correct['CORRECT_EN_US'][Math.floor(Math.random() * correct['CORRECT_EN_US'].length)] + '</say-as><break time="1s"/> your reply is correct.<break time="2s"/>',

But that does not work, to be honest I don't know how to retrieve a random value out of the (multidimensional?) array.

Can anyone lead me to the right code? Thank you!

Elisa

Adrian
  • 8,271
  • 2
  • 26
  • 43
elisa
  • 33
  • 4
  • Possible duplicate of [Get random number from multidimensional array](https://stackoverflow.com/questions/25010032/get-random-number-from-multidimensional-array) – Tomm Oct 30 '17 at 10:59
  • Take a look at the marked correct answer : https://stackoverflow.com/questions/25010032/get-random-number-from-multidimensional-array – Tomm Oct 30 '17 at 10:59

1 Answers1

2

This isn't a multi-dimensional array, what you have is an object with properties containing arrays. If the code you posted is your code, then it's fine and you only have an extra comma (,) inside the module.exports object.

Check my JSFiddle for example.

Adrian
  • 8,271
  • 2
  • 26
  • 43
  • 1
    Thank you for your fast help! I did not know how to search for the right solution as you see I did not know how to name what I'm doing. It works now! – elisa Oct 30 '17 at 11:49