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

Find Maximum and Sort Numbers

CODING QUESTION

Uploaded by

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

Find Maximum and Sort Numbers

CODING QUESTION

Uploaded by

rockysash17
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Biggest of three Numbers

<html>
<head>
<title> Biggest of three Numbers</title>
<script type="text/javascript">
var a,b,c;
a=parseInt(prompt("Enter A:"));
b=parseInt(prompt("Enter B:"));
c=parseInt(prompt("Enter C:"));
alert("Maximum val:"+maximum(a,b,c)); // passing arguments to the function
function maximum(x,y,z)
{
return [Link](x,y,z); // [Link] function returns maximum value in the function
}
</script>
</head>
<body>
</body>
</html>
Number Sorting Using Array

<!DOCTYPE html>
<html>
<head>
<title>Simplified Number Sort</title>
</head>
<body>
<h3>Enter numbers one by one</h3>
<input type="number" id="numInput" placeholder="Enter a number">
<button onclick="addNumber()">Add</button>
<button onclick="sortNumbers()">Sort</button>
<button onclick="clearAll()">Clear</button>
<p id="numbersList"></p>
<p id="output"></p>
<script>
let numbers = [];
function addNumber() {
let val = [Link]("numInput").value;
if (val) {
[Link](+val); // +val converts to number
[Link]("numbersList").innerText = "Numbers: " + [Link](", ");
[Link]("numInput").value = ""; // clear input
}
}
function sortNumbers() {
if (![Link]) return alert("No numbers entered!");
[Link]("output").innerHTML =
"Ascending: " + [...numbers].sort((a,b)=>a-b).join(", ") + "<br>" +
"Descending: " + [...numbers].sort((a,b)=>b-a).join(", ");
}
function clearAll() {
numbers = [];
[Link]("numbersList").innerText = "";
[Link]("output").innerText = "";
}
</script>
</body>
</html>
Explanation:

<input type="number" id="numInput" placeholder="Enter a number">


Creates a box where the user can enter a number.​

●​ type="number" → only allows numeric input.​

●​ id="numInput" → gives it an identifier so JavaScript can read the value.​

●​ placeholder="Enter a number" → shows grey hint text inside until the user
types.

id="numbersList" → shows the numbers entered so far.​

id="output" → shows ascending/descending sorted results.

let numbers = [];


●​ Creates an empty array to store the numbers entered by the user.

Function 1: addNumber()

○​ Reads the value from the input box (val).​

○​ if (val) → makes sure it’s not empty.​

○​ [Link](+val) → adds it to the array.​

■​ The + sign converts the string into a number ("5" → 5).​

○​ Updates <p id="numbersList"> to display all numbers entered (join(", ") adds
commas).​

○​ Clears the input box for the next entry.

Function 2: sortNumbers()
●​ Checks if numbers is empty (![Link]). If yes → shows an alert and stops.​

●​ [...numbers] → makes a copy of the array (so the original isn’t changed).​
●​ .sort((a,b)=>a-b) → sorts numbers ascending.​

●​ .sort((a,b)=>b-a) → sorts numbers descending.​

●​ .join(", ") → converts array into a string like 5, 10, 20.​

●​ Puts both results inside <p id="output"> with a <br> line break.

You might also like