javascript - How to get my anonymous function to return a value instead of a function? -


my code:

var host_choice = function(){     var choice;     {         choice = shuffled_list[random_choice()];     } while (choice == "car" || choice == player_choice);      return choice;   }; 

here host_choice function have call. unsure how make variable containing choice. know missing basic here, how it?

side note: shuffled_list contains 3 elements. , want host_choice variable contain neither element car nor player_choice there "fancier" way of doing this?

you can invoke directly.

var host_choice = (function(){   var choice;   {       choice = shuffled_list[random_choice()];   } while (choice == "car" || choice == player_choice);    return choice;   })(); 

this example of iife (immediately invoked function expression). handy various reasons, such modularity in javascript.


Comments

Popular posts from this blog

python - RuntimeError: can't re-enter readline -

python - PyInstaller UAC not working in onefile mode -

php - Need to store a large amount of data in session with CI 3 but on storing large data in session it is itself destorying automatically -