jeudi 7 décembre 2017

getting a random value of object property and storing to variable

I am trying to get a random value from the object property by comparing properties to the value I get from list item by .text(). I try to store the result to a span after that.

Not sure if the function displayWord is correct or maybe I am missing something else.

Here is some other relevant parts of the code to see what variables referring to. http://ift.tt/2AkkG0O

let result = "";
let charadesList = {
  tv : {
    easy : ['Harry Potter', 'James Bond', 'transformers'],
    medium : ['the planet of The Apes', 'Inception', 'finding nemo'],
    hard : ['taken', 'alladin', 'cast away', 'schindler\'s list']
          },

  animals : {
    easy : ['chicken', 'mouse', 'dog'],
    medium : ['lion', 'elephant', 'giraffe'],
    hard : ['monkey', 'panda', 'mole']
  }
}

function displayWord(obj, value1, value2) {
  for (var p in obj) {
    if (obj.p == value1) {
      for (var pp in obj.p) {
        if (obj.p.pp == value2) {
          result += obj.p.pp[Math.floor(Math.random()*obj.p.pp.length)];
        }
      }
    }
  }
  $('#word').append(result);
}
$(document).ready(function () {
   displayWord(charadesList, selectCategory, selectLevel);
});



Aucun commentaire:

Enregistrer un commentaire