dimanche 4 avril 2021

How to assign function's result to javascript template literal?

I'm stuck i don't know what to do anymore. So i have this code when i load console.log(chickens) in the console it really works and return a random number match to the answer (whereUat[chickens]) but when i assign it to 'answerIndex' : ${chickens} it didn't work but when i assign a normal integer like 'answerIndex' : ${2} it works.. Please reply this if you know i need ur help :) i just want to assign a random index for the answer if there's better alternative u can tell me.. btw this is my code :

kabataku = () => {
if(operator == '*'){
    multiply()
}
if(operator == '/'){
    divide()
}
if(operator == '+'){
    add()
}
if(operator == '-'){
    substract()
}

x = () => {
    return chickens = Math.floor(Math.random() * whereUat.length)
}

x()

whereUat[chickens] = answer

}

makeQuestion = () => {
    questions = [];
    for(var i = 0; i < 4; i++){
        num1 = random(),
        num2 = random(),
        opt1 = random(),
        opt2 = random(),
        opt3 = random(),
        opt4 = random(),
        whereUat = [opt1, opt2, opt3, opt4],
        operator = operations[randomOperator()]
        kabataku()
        var obj = {
        'question': `Berapakah ${num1} ${operator} ${num2} ?`, 
        'choice1' : `${whereUat[0]}`, 
        'choice2': `${whereUat[1]}`,
        'choice3': `${whereUat[2]}`,
        'choice4': `${whereUat[3]}`,
        'answerIndex' : `${chickens}`
        }
        questions.push(obj)
    }
}



Aucun commentaire:

Enregistrer un commentaire