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
Post a Comment