0

So i have this array:

var musicQuiz = [
  {
    lyrics: "Now the drugs don´t work",
    answers: [
      "The Verve",
      "Oasis",
      "Adele",
      "Rolling Stones",
      "Chris de Burgh",
      "Ceasars"
    ]
  },
  {
    lyrics: "Go your own way",
    answers: [
      "The Chemical Brothers",
      "U2",
      "The Doors",
      "Fleetwood Mac",
      "Moloko",
      "The Beatles"
    ]
  }
];

I want to show the "lyrics" randomly.so I have this:

for (var i = 0; i < musicQuiz.length; i++) {
  var question = document.getElementById("question");
  var random = Math.floor(Math.random() * musicQuiz[i].lyrics.length);
  question.textContent = musicQuiz[i].lyrics[random];
}

But it's not working, it only shows a letter. I saw how to do this with an array, but can't figure it out to do it like this. Any help and explanation would be good.

0

3 Answers 3

6

You go over the lyrics and take a random character from each of them and set it to the DOM, so at the end the page will contain a random char from the last lyric. You want to take a random lyric instead:

 var question = document.getElementById("question");
 var random = Math.floor(Math.random() * musicQuiz.length);
 question.textContent = musicQuiz[random].lyrics;
Sign up to request clarification or add additional context in comments.

3 Comments

I think the OP wants the questions listed randomly without repeats. Seems like a good application for shuffling the musicQuiz array. underscore.js provides a fisher-yates _.shuffle()
@danh yup probably helpful to the OP, not sure how that is related to my answer ...
Was suggesting a way your answer could be more helpful to the OP. I proposed it myself
0

Shuffle the array first (you can also shuffle the answers). Then enumerate it as you have, inserting shuffled elements into the dom...

let quiz = [
  { lyrics: 'hey jude, whats up with you?',
    answers: [ 'ac/dc', 'the beatles', 'queen' ]
  },
  { lyrics: 'walk like an egyption',
    answers: [ 'guns and roses', 'the bengals', 'bee gees' ]
  },
  { lyrics: 'heard it from a friend who, heard it from a friend who',
    answers: [ 'led zepplin', 'reo speedwagon', 'the eagles' ]
  },
];

let shuffledQuiz = _.shuffle(quiz);
shuffledQuiz.forEach(quizElement => {
      console.log(quizElement.lyrics);
      // insert into your dom here
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

Comments

0

Split your Lyrics into an array

    for (var i = 0; i < musicQuiz.length; i++) 
        {
           var question = document.getElementById("question");
           var lyricsArray = musicQuiz[i].lyrics.split(" ");
           var random = Math.floor(Math.random() * lyricsArray.length);
           question.textContent = lyricsArray[random];
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.