Hello,
I am trying to create a java script code that can arranges the input lowest, highest, finding average, sort them and the clear form functionality. I managed to create the code that sorts the number input from lowest to highest but I am stuck on the rest. See the code below Please help.
<!DOCTYPE>
<html>
<head>
<title>EXAM01_01</title>
<style type="text/css">
form{color:black;background-color:lightgray;border:6px solid
black;border-width:4px;width:450px;margin:1px;padding:1px;}
#ans1,#ans2,#ans3,#ans4,#numbers{background-color:white;border:4px
solid black;}
input[type="button"]{color:black;background-color:red;border:4px
solid black;}
input[type="text"]{margin:2px;}
div.title{color:white;background-color:black;float: left; width:
450px; text-align:center;}
</style>
<script>
function readNumbers()
{
var
x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var
i=0;i<array.length;i++)
{
if(
array[i]!="") array2.push(array[i]);
}
var
y=array2.sort(function (a,b){return a-b;});
alert(y.toString());
}
function readHighest()
{
var
x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var
i=0;i<array.length;i++)
{
if(
array[i]!="") array2.push(array[i]);
}
var
y=array2.sort(function (a,b){return a-b;});
alert(y.toString());
}
function readAverage()
{
var
x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var
i=0;i<array.length;i++)
{
if(
array[i]!="") array2.push(array[i]);
}
var
y=array2.sort(function (a,b){return a-b;});
alert(y.toString());
}
function readSort()
{
var
x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var
i=0;i<array.length;i++)
{
if(
array[i]!="") array2.push(array[i]);
}
var
y=array2.sort(function (a,b){return a-b;});
alert(y.toString());
}
function readClear()
{
var
x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var
i=0;i<array.length;i++)
{
if(
array[i]!="") array2.push(array[i]);
}
var
y=array2.sort(function (a,b){return a-b;});
alert(y.toString());
}
</script>
</head>
<body>
<form>
<div
class="title"><label>EXAM01_01</label></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>Enter
Numbers:</label></div><div
style="float:left;"><input type="text" id="numbers"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>Sorted
List:</label></div><div
style="float:left;"><input type="text" id="ans4"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>Lowest:</label></div><div
style="float:left;"><input type="text" id="ans1"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>Highest:</label></div><div
style="float:left;"><input type="text" id="ans2"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>Average:</label></div><div
style="float:left;"><input type="text" id="ans3"
/></div>
<p style="clear:both;" />
<div style="text-align:center;">
<input type="button" value="Lowest"
onclick="readNumbers();" />
<input type="button" value="Highest"
onclick="readHighest();"/>
<input type="button" value="Average"
onclick="Average();"/>
<input type="button" value="Sort"
onclick="Sort();"/>
<input type="button" value="Clear"
onclick="Clear();"/>
</div>
</form>
</body>
</html>
Here is a code to do so:
Here is the sample output of how my code runs.
IF the answer helped you please upvote and in case you
have any doubts post a comment, i will
surely help you out.
Code:
Hello,
I am trying to create a java script code that can arranges the input lowest, highest, finding average, sort them and the clear form functionality. I managed to create the code that sorts the number input from lowest to highest but I am stuck on the rest. See the code below Please help.
<!DOCTYPE>
<html>
<head>
<title>EXAM01_01</title>
<style type="text/css">
form{color:black;background-color:lightgray;border:6px solid black;border-width:4px;width:450px;margin:1px;padding:1px;}
#ans1,#ans2,#ans3,#ans4,#numbers{background-color:white;border:4px solid black;}
input[type="button"]{color:black;background-color:red;border:4px solid black;}
input[type="text"]{margin:2px;}
div.title{color:white;background-color:black;float: left; width: 450px; text-align:center;}
</style>
<script>
function readLowest()
{
// extract the numbers from the text box
var x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var i=0;i<array.length;i++)
{
if( array[i]!="") array2.push(array[i]);
}
// initialise the minimum value variable with the first element
// then iterate through the entire array, if any element smaller
// than the minimumValue appears, change the minimum to that.
var minimumValue = parseInt(array2[0]);
for(var i=1;i<array2.length;i++)
{
if( parseInt(array2[i])<minimumValue)
minimumValue = parseInt(array2[i]);
}
// show that result in the text box.
document.getElementById("ans1").value = (minimumValue.toString());
}
function readHighest()
{
// extract the numbers from the text box
var x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var i=0;i<array.length;i++)
{
if( array[i]!="") array2.push(array[i]);
}
// initialise the minimum value variable with the first element
// then iterate through the entire array, if any element smaller
// than the minimumValue appears, change the minimum to that.
var maximumValue = parseInt(array2[0]);
for(var i=1;i<array2.length;i++)
{
if( parseInt(array2[i])>maximumValue)
maximumValue = parseInt(array2[i]);
}
// show that result in the text box.
document.getElementById("ans2").value = (maximumValue.toString());
}
function readAverage()
{
var x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var i=0;i<array.length;i++)
{
if( array[i]!="") array2.push(array[i]);
}
// store the total in an variable and then divide it by the number of
// elements, to get the average.
var total = 0.0;
for(var i=0;i<array2.length;i++)
{
total = total + parseInt(array2[i]);
}
var average = total/array2.length;
// show that result in the text box.
document.getElementById("ans3").value = (average.toString());
}
function readSort()
{
var x=document.getElementById("numbers");
var numbers=x.value.trim();
var array=numbers.split(" ");
var array2=[];
for(var i=0;i<array.length;i++)
{
if( array[i]!="") array2.push(array[i]);
}
var y=array2.sort(function (a,b){return a-b;});
document.getElementById("ans4").value = (y.toString());
}
function readClear()
{
document.getElementById("numbers").value = "";
document.getElementById("ans1").value = "";
document.getElementById("ans2").value = "";
document.getElementById("ans3").value = "";
document.getElementById("ans4").value = "";
}
</script>
</head>
<body>
<form>
<div class="title"><label>EXAM01_01</label></div>
<p style="clear:both;" />
<div style="float:left;width:150px;"><label>Enter Numbers:</label></div><div style="float:left;"><input type="text" id="numbers" /></div>
<p style="clear:both;" />
<div style="float:left;width:150px;"><label>Sorted List:</label></div><div style="float:left;"><input type="text" id="ans4" /></div>
<p style="clear:both;" />
<div style="float:left;width:150px;"><label>Lowest:</label></div><div style="float:left;"><input type="text" id="ans1" /></div>
<p style="clear:both;" />
<div style="float:left;width:150px;"><label>Highest:</label></div><div style="float:left;"><input type="text" id="ans2" /></div>
<p style="clear:both;" />
<div style="float:left;width:150px;"><label>Average:</label></div><div style="float:left;"><input type="text" id="ans3" /></div>
<p style="clear:both;" />
<div style="text-align:center;">
<input type="button" value="Lowest" onclick="readLowest();" />
<input type="button" value="Highest" onclick="readHighest();"/>
<input type="button" value="Average" onclick="readAverage();"/>
<input type="button" value="Sort" onclick="readSort()"/>
<input type="button" value="Clear" onclick="readClear();"/>
</div>
</form>
</body>
</html>
Get Answers For Free
Most questions answered within 1 hours.