#Quizzical Solo Project - Insert Correct Answer Randomly

9 messages · Page 1 of 1 (latest)

median pagoda
#

Hey guys, im having some serious trouble with this, i got stuck with this solo project 6 months ago, i decided to keep it aside and get to the Getting hired section first, and then came back, (maybe the new stuff learned will help in the future).

I tried with .splice, combining .map with randomIndex and .splice, but i dont seem to find the solution.

I like to knock down walls with headbutts, as a way of being, but didnt work for me this time, maybe you guys can give me some “hints”, or Don’t give me the fish, teach me how to fish better said.

How do you guys proceed when you are stuck forever?

This is the scrim: https://scrimba.com/s07sgiosdm/head

snow sparrow
#

First of all - don't get "stuck forever". Ask for help, like you have here. There's only so long you can "bang your head" before it becomes unproductive.

Now for your problem right now...

Suggest you put a breakpoint in your code and step through it to see what's happening.

At what point does it "break"?

Spoiler: ||item.incorrect and item.correct don't exist...||

I'm also not 100% what that line of code is supposed to do... can you explain on your intention?

median pagoda
#

Wow the step by step check everything worked like a charm, i think i got something that should work now:

#

const allAnswersArr = questionsArr.map(item => {
const randomIndex = Math.floor(Math.random() * 3)

const updatedArr = []

updatedArr.push({
    incorrect: item.incorrect_answers
})
updatedArr.splice(randomIndex, 0, {correct: item.correct_answer})

return updatedArr

})

#

got this as a result

#

i'm gonna keep working, see if i can get further

median pagoda
#

Good night Bill, i managed to create an array of arrays with the correct answer inserted at random and select each answer radio input individually. But, probably because it is an array of arrays i get this (picture), i tried .flatMap() but it gets worse

#

// Array with correct & incorrect answers
const allAnswersArr = questionsArr.flatMap(item => {
const randomIndex = Math.floor(Math.random() * (item.incorrect_answers.length + 1))

const updatedArr = item.incorrect_answers.map(answer => answer)

updatedArr.splice(randomIndex, 0, item.correct_answer)

return updatedArr

})

console.log(allAnswersArr)