You are on page 1of 4

Name: Birari chetan prakash Roll No: 08

Div: B Batch: A1

ASSIGNMENT :-1

Write a CSMASCRIEPT program that will return 1 if the array is sorted in ascending order ,-1 if it is
sorted in descending order or 0 if it is not sorted.

Code:

<!DOCTYPE html>

<html>

<head>

<title>check array order</title>

<style>

.container {

display: flex;

flex-direction: column;

align-items: center;

.input-container {

display: flex;

flex-direction: column;

align-items: center;

margin-bottom: 20px;

input[type="text"] {

margin: 10px;

#result {

width: 200px;

}
</style>

</head>

<body bgcolor="#9FE2BF">

<div class="container">

<div class="input-container">

<label for="arr">Enter the numbers:</label>

<input type="text" id="arr" name="arr">

<input type="button" value="check" onclick="checkorder()">

</div>

<input type="text" id="result" name="result">

</div>

<script>

function checkorder() {

const arrstr = document.getElementById("arr").value;

const arr = arrstr.split(" ").map((num) => Number(num.trim()));

let isAscending = true;

let isDescending = true;

for (let i = 1; i < arr.length; i++) {

if (arr[i] < arr[i - 1]) {

isAscending = false;

if (arr[i] > arr[i - 1]) {

isDescending = false;

}
let result;

if (isAscending) {

result = 1;

} else if (isDescending) {

result = -1;

} else {

result = 0;

document.getElementById("result").value = result;

</script>

</body>

</html>

Output:

Ascendind order :

Descending Order:
Unsorted array:

You might also like