Whenever I execute this snippet the console.log before return returns the array with 20 times the value 23. However console.log(Check(users, 0, 20)); returns only 'undefined'.
What am I doing wrong?
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log(Check(users, 0, 20));
function Check(ids, counter, limit){
ids.push(23);
// Recursion
if (counter+1 < limit){
Check(ids, counter+1, limit);
}
else {
console.log(ids);
return ids;
}
}
returnstatement in theifblock meansundefined. It might be easier to maintain if you put onereturnstatement at the end of the function and set the value to return based on theifstatement