Есть ли вообще определить объекты в массиве по номерам - PullRequest
0 голосов
/ 24 апреля 2020

Я делаю разлад. js Команда пустяков. но я не понимаю Пока все работает, я хочу сделать конвертер для ответов. Как: Ответы будут числом вместо полного ответа.

Я искал везде, но не могу найти правильный ответ.

Код:

module.exports.run = async (client, message, args) => {
    
    request.get("https://opentdb.com/api.php?amount=1&type=multiple", (err, response, body) => {
        let res = JSON.parse(body)
        let results = res.results[0]

        let answers = results.incorrect_answers;
        answers.push(results.correct_answer) // Pushes the correct answers in the incorrect answers array.


        // Entity decode loop
        var i;
        for (let i = 0; i < answers.length; i++) {
            entities.decode(answers[i])  
        }

     
        shuffle(answers) // Shuffles the answers in a random order.'

        console.log(answers)

        let embed = new Discord.MessageEmbed()
            .setAuthor(`${message.author.username} trivia vraag`, message.author.displayAvatarURL())
            .setTitle(entities.decode(results.question))
            .setDescription(`1) ${answers[0]} \n 2) ${answers[1]} \n 3) ${answers[2]} \n4) ${answers[3]}`)
            .addField("Prijs", `\n ja wat Corona bacteriën.`, true)
            .addField(`Difficulty`, `\n ${results.difficulty}`, true)
            .addField(`Categorie`, `\n ${results.category}`, true)
            
            message.channel.send(embed)
            // console.log(results.question)
            // console.log(results.correct_answer)
    })



}

function shuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;

    while (0 !== currentIndex) {

        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}

Если кто-то может мне помочь, пожалуйста, мне это нужно, ха-ха. Если вы не получили его, посмотрите картинку, которую я добавил. Сообщение Discord, которое я получил, я хочу преобразовать полный ответ в число.

С уважением,

Тони

1 Ответ

0 голосов
/ 26 апреля 2020

Я немного сбит с толку, но думаю, что понял,

answers.findIndex(f => f === results.correct_answer);

это будет основано на 0 индексах, поэтому добавьте 1 к нему.

...