0% found this document useful (0 votes)
19 views4 pages

Assignment 1

The document contains a JavaScript program that checks whether an array is sorted in ascending order, descending order, or unsorted. The program takes user input of numbers in an array, separates them into individual numbers, and checks if the numbers are in ascending order, descending order, or neither by comparing adjacent numbers. It outputs 1 if ascending, -1 if descending, or 0 if unsorted.

Uploaded by

rjadhav0075
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Assignment 1

The document contains a JavaScript program that checks whether an array is sorted in ascending order, descending order, or unsorted. The program takes user input of numbers in an array, separates them into individual numbers, and checks if the numbers are in ascending order, descending order, or neither by comparing adjacent numbers. It outputs 1 if ascending, -1 if descending, or 0 if unsorted.

Uploaded by

rjadhav0075
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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