function isSortedAndHow(array) {
let array_copy=array.slice(0,array.length);
let ascending_arr=array.sort((a,b)=>{return a-b});
let descending_arr=array.sort((a,b)=>{return b-a});
//for checking array equality
function element_check(arr1,arr2){
return arr1.every((a1,a2)=>{a1===arr2[a2]})
}
if(element_check(array,ascending_arr)){
return "yes,ascending order";
}
else if(element_check(array,descending_arr)){
return "yes,descending order";
}
else{
return "no";
}
}
I'm trying to accept an array, check if it is sorted in any order and return output. However,the code is returning no all the time.
arr1.every. btw, it is funny to use arrow functions and then a block with return instead of simple returning a value.> ((a) => 1)(5) 1 > ((a) => {1})(5) undefinedarray_copybut don't use it at all.ascending_arr,descending_arr, andarrayare all the same array.element_checkwas correct it would always returnyes, ascending ordersince all 3 names refer to the same object. Given that it is incorrect it doesn't.