I have the following code:
var disArray = ['red','red','green','green','green','blue','blue','blue','blue','blue'];
var otherArray = [];
function takeOut() {
for ( i = 0; i < 3; i++ ) {
var randItem = disArray[Math.floor(Math.random()*disArray.length)];
otherArray.push(randItem);
}
return otherArray;
}
takeOut(disArray)
console.log(otherArray)
I want the function to return the elements in otherArray when it is called, but it I get the error undefined. It only works when I console.log otherArray. Is there any way that I can make the function return the array without using console.log?
otherArrayis not in the scope of the function.undefinedis not an error. You don’t even get any error.console.log. I'm not able to reproduce theundefinedvalue. Just doconsole.log(takeOut(disArray))