CrazyMatt

10-18-2006, 07:53 AM

Alright I have created some code that works as far as random number loading. But i am having difficulty trying to create code that will find the location of the smallest number. Here is the prompt I am giving to clear things up and below is the code i created.

Prompt:Write a JavaScript function called "findSmallest" that takes two parameters: an array of numbers, and the size of the array. The function returns the index of the smallest element (note that I am not asking you to return the smallest value itself... I want the function to tell me where in the array the smallest value is found)

Notice this is only a piece of what I have to do. There is so much more but this is the one thing troubling me. Can anyone help me? if it is easier to email me. than email me at mattkaczco2006@gmail.com. Thank you

<html>

<head>

<title>random</title>

<script type="text/javascript">

<!--

function exam1 ( ) {

var max = 25;

var arr = new Array(max);

var sLoc = -1; // just an initial value

getData(arr); // assume this function loads the array with

// integers randomly selected from the

// range 0 to 99 inclusive

document.writeln("<h2>Random original data</h2>");

for (i=0; i<max; i++) {

document.write(" "+arr[i]);

}

// sLoc = findSmallest(arr,max);

// you stomp the -1 init value with the real location

alert("smallest value is here: "+sLoc);

}

function getData (arr) {

var i;

for (i=0; i<arr.length; i++) {

arr[i] = Math.floor(100*Math.random());

}

}

// write your function findSmallest to go here

-->

</script>

</head>

<body>

<form name="f" action="">

<input name="b3" type="button" value="go do it"

onclick="exam1()" >

</form>

</body>

</html>

Prompt:Write a JavaScript function called "findSmallest" that takes two parameters: an array of numbers, and the size of the array. The function returns the index of the smallest element (note that I am not asking you to return the smallest value itself... I want the function to tell me where in the array the smallest value is found)

Notice this is only a piece of what I have to do. There is so much more but this is the one thing troubling me. Can anyone help me? if it is easier to email me. than email me at mattkaczco2006@gmail.com. Thank you

<html>

<head>

<title>random</title>

<script type="text/javascript">

<!--

function exam1 ( ) {

var max = 25;

var arr = new Array(max);

var sLoc = -1; // just an initial value

getData(arr); // assume this function loads the array with

// integers randomly selected from the

// range 0 to 99 inclusive

document.writeln("<h2>Random original data</h2>");

for (i=0; i<max; i++) {

document.write(" "+arr[i]);

}

// sLoc = findSmallest(arr,max);

// you stomp the -1 init value with the real location

alert("smallest value is here: "+sLoc);

}

function getData (arr) {

var i;

for (i=0; i<arr.length; i++) {

arr[i] = Math.floor(100*Math.random());

}

}

// write your function findSmallest to go here

-->

</script>

</head>

<body>

<form name="f" action="">

<input name="b3" type="button" value="go do it"

onclick="exam1()" >

</form>

</body>

</html>