1

I'm looking for advice on whether this is the best data structure for what Im trying to achieve, and if so, how I would create a function that chooses a song at random.

var song = {

    john: [ "Come Together", "Revolution" ],
    paul: [ "Hey Jude", "Blackbird" ],
    george: [ "Something", "Here Comes the Sun" ],
    ringo: [ "Yellow Submarine", "With a Little Help from my Friends" ]
};

Here's how it will work:

  1. Timer begins when the user clicks start
  2. A song (picked at random), is displayed onscreen
  3. User then clicks one of four icons/names (john, paul, george or ringo)
  4. Answer is logged (correct or incorrect).
  5. Another song is picked at random until the timer finishes.

I don't want a solution to this problem, I just want to know if I'm using the best data structure...and if I am (or if I'm not!), how I'd go about generating a random song. I'm new to this and what I'm trying to do is way more complicated than anything I've tried before. Final point...John and Paul's individual arrays are going to be pretty damn long when the song list is complete...

Thanks!

5
  • I think this is a better question for softwareengineering.stackexchange.com Commented Nov 18, 2016 at 17:19
  • 3
    I think using an array of songs that have two properties: title and author would make it easier for your purposes. Commented Nov 18, 2016 at 17:19
  • Do you always have exactly two songs per artist? Are you wanting there to be an equal probability that any song is chosen, or that any artist is chosen (in cases where artists have different numbers of songs) (and then a random song from that artist)? From a testing point of view it should be that there is an equal probability of selecting each artist regardless of the number of songs per artist. If that is not the case, then guessing the artist based on the one with the most songs can become a viable test taking strategy. Commented Nov 18, 2016 at 17:46
  • I intend to put the full Beatles back catalogue into the array, so it will be hundreds of songs long (the game will actually track who is on lead vocal, not authorship of the song). John and Paul will be have the vast majority, so ringo and george's arrays will be quite short in comparison. I did think about having author:title as the the key:value, but I was thinking ahead and trying to keep the code as short as possible - if john has 200 songs say, I'd have to write those key:value pairs out for each song - the way I wrote the array above, each vocalist only gets written once. Commented Nov 18, 2016 at 17:54
  • @SumnerEvans when referring other sites, it is often helpful to point that cross-posting is frowned upon Commented Nov 18, 2016 at 18:16

2 Answers 2

1

I would recommend using a relational structure.

var artists = ['john', 'paul', 'george', 'ringo'];
var songs = [
  { name: "Come Together", artist: 0 },
  { name: "Revolution", artist: 0 },
  { name: "Hey Jude", artist: 1 },
  { name: "Blackbird", artist: 1 },
  { name: "Something", artist: 2 }
];
  1. This allows you to add more properties to the artists if you need (like a picture, wikipedia link, etc).
  2. It allows a faster lookup of the artist given a song. Your current setup would require a big cost to find the artist given a song.
  3. It is easier to randomly, uniformly, pick a random song. Your current setup would make it difficult to pick a song randomly and uniformly.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Joe. I'm actually a fair way of testing this yet but this seems to make sense.
1

I would use an array of songs, each of which has an author:

You could then find all of the songs with a given author using filter():

var songs = [{
  author: 'john',
  title: 'Come Together'
}, {
  author: 'paul',
  title: 'Hey Jude'
}, ];


var johnsSongs = songs.filter(function(song) {
  return song.author === 'john';
});
             
console.log(johnsSongs);

1 Comment

Thanks Sumner. I think the problem with this solution is if my song list runs into the 100s, then I have to write those author:title pairs for each song.

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.