Define a generic method called checkOrder() that checks if four items are in ascending, neither, or descending order. The method should return -1 if the items are in ascending order, 0 if the items are unordered, and 1 if the items are in descending order.

Respuesta :

tonb

Answer:

function checkOrder(arr) {

const asc = [...arr].sort();

if (arr.toString() === asc.toString()) return -1;

const desc = [...arr].sort((a,b) => b-a);

if (arr.toString() === desc.toString()) return 1;

return 0;

}

function test(arr) {

switch(checkOrder(arr)) {

 case -1:

  console.log(`${arr}: these numbers are ascending`);

  break;

 case 0:

  console.log(`${arr}: these numbers are unordered`);

  break;

 case 1:

  console.log(`${arr}: these numbers are descending`);

  break;  

}  

}

test([1,2,3,4]);

test([7,5,3,1]);

test([2,3,1,7]);

Explanation:

This is a solution using javascript.