0

I have made this program in python, and wanted to achieve the same results in Javascript. Unfortunately I am struggling with one final piece which is is a person is added to the final 'barge' array, I don't want them to be used again, as in I cannot have duplicate people in the final array. any suggestions on what I'm missing. Again in python, I got this nailed down, but javascript I cant seem to get the duplicates to stop. Also open to any help with this program as a whole. Thanks!

var a = ['mike', 'dan', 'christian', 'trevor', 'jess'];
var b = ['mike', 'sarah', 'kenny', 'trevor', 'dennis', 'jeff'];
var c = ['pavel', 'sean', 'kenny', 'chris', 'lucas', 'elizibeth'];
var d = ['sarah', 'sean', 'allison', 'jules', 'trevor', 'jeff'];
var e = ['sarah', 'sean', 'jules', 'trevor', 'chris'];
var f = ['val', 'kirk', 'pavel', 'sean', 'kenny', 'chris', 'lucas', 'elizibeth'];
var g = ['mike', 'sarah', 'kenny', 'trevor', 'dennis', 'jeff'];
var h = ['val', 'sarah', 'allison', 'kenny', 'trevor', 'lucas'];


function randomChoice(list) {
  return (list[Math.floor(Math.random() * list.length)]);
};
var barge = []
while (barge.length < 8) {
  var barge = []
  var used = []
  var alpha = randomChoice(a);
  if (!(alpha in used)) {
    barge.push(alpha);
    used.push(alpha);
    var bravo = randomChoice(b);
    if (!(bravo in used)) {
      barge.push(bravo);
      used.push(bravo);
      var charlie = randomChoice(c);
      if (!(charlie in used)) {
        barge.push(charlie);
        used.push(charlie);
        var delta = randomChoice(d);
        if (!(delta in used)) {
          barge.push(delta);
          used.push(delta);
          var echo = randomChoice(e);
          if (!(echo in used)) {
            barge.push(echo);
            used.push(echo);
            var foxtrot = randomChoice(f);
            if (!(foxtrot in used)) {
              barge.push(foxtrot);
              used.push(foxtrot);
              var golf = randomChoice(g);
              if (!(golf in used)) {
                barge.push(golf);
                used.push(golf);
                var hotel = randomChoice(h);
                if (!(hotel in used)) {
                  barge.push(hotel);
                  used.push(hotel);
                }
              }
            }
          }
        }
      }
    }
  }
};
console.log(barge);

1 Answer 1

3

An easy solution would be to use a Set.

var set = new Set(); 
set.add('mike');
set.add('dan');
...

Sets contain unique values, so you'll have (at most) one instance of each name.

Sign up to request clarification or add additional context in comments.

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.