Trying to put together an HTML/Javascript page that includes the following: The
ID: 3547376 • Letter: T
Question
Trying to put together an HTML/Javascript page that includes the following:
The user clicks the button at the bottom, which prompts the user to enter four numbers, one at a time.
These numbers are put into an ARRAY.
the program then calculates the mean (average) of the numbers
the program then counts the numbers that are smaller than the mean
the program then counts the numbers that are larger than the mean
All of these are put onto the page under the button.
Here's what I have so far:
<script type="text/javascript">
function getArrayNumbers()
{
var numOne;
var numTwo;
var numThree;
var numFour;
var newMean;
var smallNumber;
document.getElementById("lbl1").innerHTML = "Your data: " + numOne + "," + numTwo + "," + numThree + "," + numFour ;
document.getElementById("lbl2").innerHTML = "The mean (average) of this data is: " + newMean;
document.getElementById("lbl3").innerHTML = smallNumber + " number(s) are smaller than the mean number.";
document.getElementById("lbl4").innerHTML = largeNumber + " number(s) are larger than the mean number.";
}
</script>
</head>
<body>
<h3>Enter 4 numbers. They will be stored in the Array.</h3>
<form name="form1">
<p><input type="Button" value="Enter numbers" /></p>
<p id="lbl1" > </p>
<p id="lbl2" > </p>
<p id="lbl3" > </p>
<p id="lbl4" > </p>
</form>
</body>
</html>
Explanation / Answer
<!DOCTYPE html>
<html>
<head>
<script>
function getArrayNumbers()
{
var i;
var nos ="" ;
var mean=0 ;
var low="";
var high="";
var myvalues = new Array();
for (i=0;i<4;i++)
{
myvalues[i] = prompt("enter number : "+(i+1));
nos = nos + myvalues[i]+ " " ;
mean = mean + Number( myvalues[i] ) ;
}
for(i=0;i<myvalues.length;i++)
{
if(Number(myvalues[i]) < (mean/4))
{
high = high + myvalues[i] + " " ;
}
if(Number( myvalues[i]) > ( mean/4 ))
{
low = low + myvalues[i] + " " ;
}
}
document.getElementById("one").innerHTML = "Entered values :- "+ nos ;
document.getElementById("two").innerHTML = "Mean of no's :- "+ mean/4 ;
document.getElementById("three").innerHTML = "Higher than Mean are :- "+high ;
document.getElementById("four").innerHTML = "Lower than Mean are :- "+ low ;
}
</script>
</head>
<body>
<h2 align="center">
<p id="one"> ----------- </p>
<p id="two"> ----------- </p>
<p id="three"> ----------- </p>
<p id="four"> ----------- </p>
<input type="button" value="please press" />
</h2>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.