I need assistance with converting the provided if else code into a function. The main goal is to transfer the arrays from local scope to global scope. However, whenever I attempt to enclose the code within a function, I encounter a reference error stating "code is not defined". It's evident that I must figure out a way to return these arrays from the local scope to the global scope.
if (randomnum == 1) {
var codes = ["qjyreu", "achin", "eralnrde", "hiwel", "eW", "ni"];
var words = ["jquery", "china", "learned", "while", "We", "in"];
var finalAnswer = "Welearnedjquerywhileinchina";
}
else if (randomnum == 2) {
var codes = ["oegs", "yob", "nignadc", "hTe", "ightn", "veery"];
var words = ["goes", "boy", "dancing", "The", "night", "every"];
var finalAnswer = "Theboygoesdancingeverynight";
}
To provide context, the random number (randomnum) is determined by the rando function:
function rando() {
return (randomnum = Math.floor(Math.random() * 2) + 1);
}
rando();