In: Computer Science
How to create a JS application using the code below that displays the lists of grades in their corresponding letter grade textbox and each grade being sorted from lowest to highest.
<!DOCTYPE>
<html>
<head>
<title>EXAM01_03</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,#ans5,#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>
</head>
<body>
<form>
<div
class="title"><label>EXAM01_03</label></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>Enter Grade
List:</label></div><div
style="float:left;"><input type="text" id="numbers"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>A:</label></div><div
style="float:left;"><input type="text" id="ans1"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>B:</label></div><div
style="float:left;"><input type="text" id="ans2"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>C:</label></div><div
style="float:left;"><input type="text" id="ans3"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>D:</label></div><div
style="float:left;"><input type="text" id="ans4"
/></div>
<p style="clear:both;" />
<div
style="float:left;width:150px;"><label>F:</label></div><div
style="float:left;"><input type="text" id="ans5"
/></div>
<p style="clear:both;" />
<div style="text-align:center;">
<input type="button" value="Enter" />
<input type="button" value="Clear"
/>
</div>
</form>
</body>
</html>
Below is the complete code. The inserted code has been highlighted in bold. It is well explained inside the code using comments.
<!DOCTYPE>
<html>
<head>
<title>EXAM01_03</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, #ans5, #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
type="text/javascript">
// declare array of each
grade to store grades
AGrades = [];
BGrades = [];
CGrades = [];
DGrades = [];
FGrades =
[];
// this
function will add to the corresponding array
function
fillArrays(grade) {
if (grade > 90)
AGrades.push(grade);
else if (grade > 80)
BGrades.push(grade);
else if (grade > 70)
CGrades.push(grade);
else if (grade > 60)
DGrades.push(grade);
else
FGrades.push(grade);
}
function
calculate() {
// get grades text from the grade list
gradesText = document.getElementById('numbers').value;
// check if grades text is not empty
if (gradesText.trim() == '') {
alert('Please enter the grades in the list');
}
// split the grades into an array
grades = gradesText.split(' ');
// get the corresponding letter grades and add to the
corrresponding textbox
for (var i = 0; i < grades.length; i++) {
// call function to add to corresponding array
fillArrays(grades[i]);
}
// sort the arrays
AGrades.sort();
BGrades.sort();
CGrades.sort();
DGrades.sort();
FGrades.sort();
// fill grades the corrresponding textboxes
document.getElementById('numbers').value = AGrades;
document.getElementById('ans2').value = BGrades;
document.getElementById('ans3').value = CGrades;
document.getElementById('ans4').value = DGrades;
document.getElementById('ans5').value = FGrades;
}
// this
function clears the input boxes
function clearAll()
{
document.getElementById('numbers').value = '';
document.getElementById('ans1').value = '';
document.getElementById('ans2').value = '';
document.getElementById('ans3').value = '';
document.getElementById('ans4').value = '';
document.getElementById('ans5').value = '';
}
</script>
</head>
<body>
<form>
<div
class="title"><label>EXAM01_03</label></div>
<p
style="clear:both;" />
<div
style="float:left;width:150px;"><label>Enter Grade
List:</label></div><div
style="float:left;"><input type="text" id="numbers"
/></div>
<p
style="clear:both;" />
<div
style="float:left;width:150px;"><label>A:</label></div><div
style="float:left;"><input type="text" id="ans1"
/></div>
<p
style="clear:both;" />
<div
style="float:left;width:150px;"><label>B:</label></div><div
style="float:left;"><input type="text" id="ans2"
/></div>
<p
style="clear:both;" />
<div
style="float:left;width:150px;"><label>C:</label></div><div
style="float:left;"><input type="text" id="ans3"
/></div>
<p
style="clear:both;" />
<div
style="float:left;width:150px;"><label>D:</label></div><div
style="float:left;"><input type="text" id="ans4"
/></div>
<p
style="clear:both;" />
<div
style="float:left;width:150px;"><label>F:</label></div><div
style="float:left;"><input type="text" id="ans5"
/></div>
<p
style="clear:both;" />
<div
style="text-align:center;">
<input type="button" value="Enter"
onclick="calculate()" />
<input type="button" value="Clear"
onclick="clearAll()"/>
</div>
</form>
</body>
</html>
Below is the sample output:
This completes the requirement. Let me know if you have any questions.
Thanks!