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

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -