The result that's logged to the console (answer) is the correct one, but it's coming out of the function as undefined. What is going on?
let sorted = [];
function reset(){
sorted = [];
}
function sort(list) {
let least = list[0];
for (let i = 0; i < list.length; i++){
if (list[i] < least ){
least = list[i];
}
}
sorted.push(least);
list.splice(list.indexOf(least),1);
if (list[0] !== undefined){
sort(list)
}
else {
let answer = sorted;
reset();
console.log(answer);
return answer;
}
}
let sampleArray = [3,2,1];
sort(sampleArray);
ifbranch you have noreturn, which means you get the default return value (undefined).