View Full Version : Array Help Mostly

10-18-2006, 08: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

<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


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

<input name="b3" type="button" value="go do it"
onclick="exam1()" >



10-18-2006, 09:16 AM
basically, as the order of the numbers is random, you're going to need to loop through them all to find the smallest.
In each iteration of the loop you'll be considering the 'current' array value and deciding if it is the 'current-smallest'
If you compare the current value to the current-smallest value, then if the former is smaller than the latter, then 'current-smallest' takes on the new value. THen move onto the next. When you've finished looping, 'current-smallest' will contain the smallest number.
As you want the index, as well as keeping track of 'current-smallest' as you're iterating, you'll want to keep track of 'current-smallest-index' as well, so you can return this.

Hope that helps

10-18-2006, 03:22 PM
Can you give me an example of the line of code.. Im actually really confused by this. I know that you have to run a loop to find the smallest value by taking one of the values in the random selection and comparing it.. How do you call a value from the random number count and compare it to another number from it?

10-18-2006, 03:30 PM
alright. Just formed this code. Is there something i should do different with this. or can you see if this is what you were talkin about?

function findSmallest(arr,max){

var secondArray = new Array(max) // declaration of new Array

secondArray = arr // making secondArray equal the normal array

var where // variable representing where the minimum value is stored

var minimumInArray = secondArray[0]; //variable used to find minimum value in array

for (k=0; k<secondArray.length; k++) {
if (secondArray[k]<minimumInArray) {
minimumInArray = secondArray[k];

for (i=0; i<max; i++) {
if (minimumInArray == arr[i]) {
where = i;
return where;