首页 > 解决方案 > 用 Alexa 数数

问题描述

我正在尝试编写一个 Alexa 意图,该意图将计算到用户给定的西班牙语英文数字。我可以让 Alexa 用西班牙语说出正确的数字,但我在试图弄清楚如何让她说出多个数字时遇到了麻烦。

这是我的代码:

    const numbers = {
    'Zero':'Cero',
    'One': 'Uno',
    'Two' : 'Dos',
    'Three': 'Tres'    
    }

    const CountUpToIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'CountUpToIntent';
    },

    handle(handlerInput) {
        const numberToCountTo = 
handlerInput.requestEnvelope.request.intent.slots.numberr.resolutions.resolutionsPerAuthority[0].values[0].value.name;
        const speechOutput = [];
        while (allTheNumbers !== 'Zero') {
            var allTheNumbers = numbers[numberToCountTo];    
            speechOutput.push(allTheNumbers--);
        }

        return handlerInput.responseBuilder
        .speak(speechOutput)
        .reprompt(speechOutput)
        .getResponse();
    }
    };

有人可以帮我正确地编写循环,所以 Alexa 说的不止一个数字吗?

谢谢

标签: node.jsloopscountalexatranslate

解决方案


我认为你做错了,你在比较allTheNumbers变量时甚至没有声明它。您可以通过使用号码槽来解决此问题的一种方法,当用户说出某个号码时,然后用给定的号码循环它。并检查给定的数字进行比较,这样更容易。

const numbers = {
    '1':'Cero',
    '2':'Uno',
    '3':'Dos',
    '4':'Tres'    
    }

推荐阅读