Click to See Complete Forum and Search --> : I'm so lost with this... (Forms, arrays, basic javascript)


JavaStrip
09-26-2003, 03:10 PM
This is a class asignment and you can probably see how little I know javascript. I don't know where to start, esspecially with this script. Everything but the javascript should be right, can anyone help? Or Explain?

<html>
<head>
<title>-</title>



</head>
<body>
<center>
<h3><b>5</b></h3>
</center>

<form name="lab05">

Type an integer into this box and hit "MakeList" to make a list of random numbers.<br>
Size of list: <input type=text name="n">
<input type=button value="MakeList" onclick="MakeList()"> <hr>

Click this button to find the largest element in the list.<br>
<input type=button value="GetMax" onclick="GetMax()">

<input type=text name="max"> <hr>

Enter a number (smaller than the size of the list) to find out the value of a specific list element.<br>
Element Number:<input type=text name="k">

<input type=button value="GetElement" onclick="GetElement()">
Result:<input type=text name="result">

</form>

<script language="JavaScript">

var list = new array();

function MakeList()

{
var i;
var n;


n = eval(document.form.n.value);


for(i=0; i<n; i++) {
list[i]= math.round(1000*math.random());
}
}


function GetMax()


{

var max;
var n;
var i;

n = eval(document.form.n.value);

max = list[0];

for(i=0; i<(n-1); i++) {
if (list[i] > max) }
{
else
max = list[i];
}


}

document.form.max.value = max;



function GetElement()


{
var k;
var n;

k = eval(document.form.k.value);
n = eval(document.form.n.value);


for(k > n) {
alert("List has only" + n + "elements.");
}
ELSE {

document.form.result.value = list[k-1];
}

}


</script>
</body>
</html>



As you can see, its a mess...

Jona
09-26-2003, 03:23 PM
Could you run through the logic, so I know exactly what you need?

[J]ona

JavaStrip
09-26-2003, 03:49 PM
Alrighty... The HTML part somewhat explains it...

Type an integer into this box and hit "MakeList" to make a list of random numbers.

Size of list [ form box ] button (enter 8, you get a list of 8 random numbers)

then

Click this button to find the largest element in the list.
button [ form box ] (when you hit the button, it displays the highest number in the list made above in the form box)

lastly

Enter a number (smaller than the size of the list) to find out the value of a specific list element.
Element Number [ form box ] button
Results [form box]


uh... I don't really get that one... but here's a quot from the instructions=

"GetElement() - Displays the kth element in the list created by MakeList(n). If k is greater than n, a warning message must be displayed. The user specifies k by typing an integer into a text box. The result is displayed in another text box. "

Hope this helps..

Jona
09-26-2003, 04:19 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html> <head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var list = new Array();

function MakeList(){
var i, n;
n = parseInt(document.lab05.n.value);
for(i=0; i<n; i++){
list[i]=Math.round(1000*Math.random());
alert(list[i]);
} }

function GetMax(){
var max, n, i;
n = parseInt(document.lab05.n.value);
max = list[0];

for(i=0; i<(n-1); i++) {
if(list[i] > max){ alert("You have exceeded the maximum limit"); return false;}
else {max = list[i];}
document.lab05.max.value = max;
}
}

function GetElement(){
var k, n;
k = parseInt(document.lab05.k.value);
n = parseInt(document.lab05.n.value);

if(k>n){alert("List has only " + n + " elements."); return false;}
else {document.lab05.result.value = list[k-1];}
}
</script>
</head>
<body>
<div style="text-align:center;">
<h3><b>5</b></h3>
</div>
<form name="lab05" action=""><div>
<p>Type an integer into this box and hit "MakeList" to make a list of random numbers.</p>
<p>Size of list: <input type=text name="n"></p>
<p><input type=button value="MakeList" onclick="MakeList()"></p>
<p>Click this button to find the largest element in the list.</p>
<p><input type=button value="GetMax" onclick="GetMax()">
<input type=text name="max"></p>
<p>Enter a number (smaller than the size of the list) to find out the value of a specific list element.</p>
<p>Element Number:<input type=text name="k"></p>
<p><input type=button value="GetElement" onclick="GetElement()"></p>
<p>Result: <input type=text name="result"></p>
</div></form>
</body>
</html>


[J]ona