Search code examples
javascriptnested-loops

How to select random links into a nested array in lab.js


I'm working on a program in lab.js for my thesis. To this point, I had some help, but now I'm on my own and I'm fairly new to programming, so I'm totally lost right now.

I have an experiment where I want to present songs to my participants, which are chosen based on their personality profile I got from a qestionnaire beforehand. My program is supposed to select the 3 highest and lowest scoring personality facettes and check which song lists correspond to them and randomly choose 4 songs from the respective lists of links for each of the 3 facettes.

I know that it gives me the right high and low scores, but it always plays songs from the very first list in my function, even if the facettes in there are not part of the highest or lowest scoring facettes.

For me it was not possible to figure out why this is and I'm hoping that somebody here can spot the mistake I've definately made somewhere. I tried renaming the functions, === instead of ==, '' instead of "", the arrays with the songs were declared as const or starting with a capital letter, I changed the names of the var in the function and I also renamed the const and var in the sorted object functions,

but it did not change the outcome. I also tried to make the function work without the ||, but then I only get an empty array for iPIPSongs and iPIPmin.

Plus, as @bloodyKnuckles pointed out, my const rng = new lab.util.Random() is undefined.

Thanks so much in advance for any input. Apologies for not correctly naming all the components, I'm still learning.

My code looks like this:

songsMNegV = [
  'https://applux05.rz.uni-kiel.de/biopsych-experiments/StimmMusik/MP31/Arcade%20Fire%20-%20Deep%20Blue.mp3',
  'https://applux05.rz.uni-kiel.de/biopsych-experiments/StimmMusik/MP31/Death%20in%20Vegas%20-%20Girls.mp3',
  'https://applux05.rz.uni-kiel.de/biopsych-experiments/StimmMusik/MP31/Ace%20of%20Base%20-%20Unspeakable.mp3', 
  'https://applux05.rz.uni-kiel.de/biopsych-experiments/StimmMusik/MP31/Radiohead%20-%20Weird%20Fishes-Arpeggi.mp3',
  'https://applux05.rz.uni-kiel.de/biopsych-experiments/StimmMusik/MP31/Jeff%20Buckley%20-%20Dream%20Brother.mp3']

 //shortened link list

function getSortedObjMax(obj) {
  var keysMax = [];
  var firstValues = [];
  for(var keyMax in obj) {
    keysMax.push(keyMax);
    firstValues.push(obj[keyMax]);
  }
  firstScores = []
  for(var i = 0; i < 3; i++) {
   const max = Math.max.apply(null, firstValues);
   const indexMax = firstValues.indexOf(max);
    firstScores.push(keysMax.splice(indexMax, 1)[0]);
    firstValues.splice(indexMax, 1)
  }
  return firstScores
}
//I use the function again to get the last scores 
   
var scores = {
  n1: window.aengstlichkeit_score,
  n2: window.reizbarkeit_score,
  n3: window.depression_score,
  n4: window.befangenheit_score,
  n5: window.impulsivitaet_score,
  n6: window.verletzlichkeit_score,
  e1: window.herzlichkeit_score,
  e2: window.geselligkeit_score,
  e3: window.durchsetzungsvermoegen_score,
  e4: window.aktivitaet_score,
  e5: window.erlebnishunger_score,
  e6: window.frohsinn_score,
  o1: window.fantasie_score,
  o2: window.aesthetik_score,
  o3: window.gefuehle_score,
  o4: window.handlungen_score,
  o5: window.ideen_score,
  o6: window.werte_score,
  a1: window.vertrauen_score,
  a2: window.freimuetigkeit_score,
  a3: window.altruismus_score,
  a4: window.entgegenkommen_score ,
  a5: window.bescheidenheit_score,
  a6: window.gutherzigkeit_score,
  c1: window.kompetenz_score,
  c2: window.ordnungsliebe_score,
  c3: window.pflichtbewusstsein_score,
  c4: window.leistungsstreben_score,
  c5: window.selbstdisziplin_score,
  c6: window.besonnenheit_score
};


window.firstScores = getSortedObjMax(scores);
   
const rng = new lab.util.Random()

function max(samples) {
window.iPIPSongs =[];
  for (var score in firstScores){
    if (score == "a1" || "a3" || "a4" || "a6" || "c3"){
      iPIPSongs.push(rng.sample(songsMLowA, samples, false));
    } else if (score == "a5"||"n4"||"n6"){
      iPIPSongs.push(rng.sample(songsMNegV, samples, false));
    } else if (score == "c6"||"o1"||"o6"){
      iPIPSongs.push(rng.sample(songsMPosD, samples, false));
    } else if (score == "e4"){
      iPIPSongs.push(rng.sample(songsU1PosV, samples, false));
    } else if (score == "e3"){
      iPIPSongs.push(rng.sample(songsU2PosV, samples, false));
    } else if (score == "a2"){
      iPIPSongs.push(rng.sample(songsSLowA, samples, false));
    } else if (score == "c1"){
      iPIPSongs.push(rng.sample(songsSPosV, samples, false));
    } else if (score == "c4"||"o2"||"o5"){
      iPIPSongs.push(rng.sample(songsSPosD, samples, false));
    } else if (score == "n1"||"o3"){
      iPIPSongs.push(rng.sample(songsINegV, samples, false));
    } else if (score == "e2"||"e5"){
      iPIPSongs.push(rng.sample(songsINegD, samples, false));
    } else if(score == "o4"){
      iPIPSongs.push(rng.sample(songsIPosV, samples, false));
    } else if(score == "n3"||"n2"){
      iPIPSongs.push(rng.sample(songsIHiA, samples, false));
    } else if (score == "c2"|| "c5"||"e6"){
      iPIPSongs.push(rng.sample(songsCLowA, samples, false)); 
    } else if (score == "e1"||"n5"){
      iPIPSongs.push(rng.sample(songsCNegD, samples, false));
    }
  }
  return iPIPSongs
}
// I use the function to also get the songlinks for iPIPmin

  window.iPIPSongs = [];
  window.iPIPmin = [];
  window.iPIPSongs = max(4);
  window.iPIPmin = min(4);

Please let me know if you need more code or explanation from me. :)


Solution

  • The Iterator was wrong - it looked like this window.firstScores = getSortedObjMax(scores); but it needs to look like this: window.firstScores = getSortedObjMax(scores).values();

    Only then the program can iterate over the if- funtion correctly. Aditionally, it needs to be for(let...of) function to work. So it is:

    function max(samples) {
    window.iPIPSongs =[];
     for (let score of firstScores){
        if (score == 'a1' || score == 'a3' || score =='a4' ||score == 'a6' || score =='c3'){
          iPIPSongs.push(rng.sample(songsMLowA, samples, false));
    // and so on

    I'm sure there are other, more elegant ways to do this, but as long as it is running correctly, its good enough for me. Thank you to everyone :)