403,849 views
37 votes
37 votes
Define a generic function called CheckOrder() that checks if four items are in ascending, neither, or descending order. The function 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. The program reads four items from input and outputs if the items are ordered. The items can be different types, including integers, strings, characters, or doubles. Ex. If the input is: bat hat mat sat 63.2 96.5 100.1 123.5 the output is: Order: -1 Order: -1

User Manuel Rauber
by
3.0k points

1 Answer

24 votes
24 votes

Answer and Explanation:

Using javascript:

/*This function checks only for ascending order. This function cannot check ascending orders for strings, just integers or floats.*/

function CheckOrder(){

var takeinput= prompt("please enter four numbers")

var makeArray= takeinput.split("");

var numArray= new Array(4);

numArray= [makeArray];

var i;

for(i=0; i<=numArray.length; i++){

var nowElem= numArray[i];

if(numArray[1]){

Alert("let's check to see");

}

else if(

nowElem > numArray[i--]){

Alert("might be ascending order");

if(numArray[i]==numArray[2]){

var almosthere=numArray[2]

}

else if(numArray[i]==numArray[3]){

var herenow=numArray[3];

if(almosthere>herenow){

Alert("numbers are in ascending order");

}

}

}

else(

Console.log("there are no ascending orders here")

)

}

The function above can further be worked on to also check for descending order of four numbers(can also be further developed to check for strings). You need just tweak the else if and nested else if statements thus :

else if(

nowElem < numArray[i--]){

Alert("might be descending order");

if(numArray[i]==numArray[2]){

var almosthere=numArray[2]

}

else if(numArray[i]==numArray[3]){

var herenow=numArray[3];

if(almosthere<herenow){

Alert("numbers are in descending order");

}

}

}

User Emelyn
by
2.8k points