Ahsan Khalid
Ahsan Khalid

Reputation: 79

Why an array comparison of sorted array in both ascending and descending in a single block of code/function results to true?

Why do ascending and descending arrays give the same output? Anyone can solve this?

function isSortedAndHow(arr) {
  let asc = arr.sort((a, b) => a - b); // Ascending order , yes
  let dsc = arr.sort((a, b) => b - a); // descending array, yes

  if (arr == asc) {
    console.log("Ascending , Yes");
  } else if (arr == dsc) {
    console.log("Descending , Yes");
  } else {
    console.log("No");
  }
}
isSortedAndHow([25,20,15,30,35]);

Upvotes: 0

Views: 19

Answers (0)

Related Questions