user966923
user966923

Reputation: 1

return functions for arrays

Need a bit of help completing a Javascript program which uses succesive functions. I cant find a way of replacing the return statement with variable so I can use the varable in the second function.

The program uses the statement findArrayparts(myArray1, myArray2) to get the function findArrayparts(myArray1, myArray2) to generate a random integer one less then the array length of one of the arrays ( equal lengths).

This random integer is then used in the function findArrayparts(myArray1, myArray2) to work out the corresponding array elements and put them into an array which is then displayed in an alert box.

<SCRIPT SRC = glossary.js></SCRIPT>
<SCRIPT language = "JavaScript">
var myVar1;
var myVar2;
var myVar3;

var myArray1 = ['red', 'orange', 'green', 'blue', 'yellow'];
var myArray2 = ['tomatoe', 'orange', 'apples', 'blueberry', 'banana'];

// generates random integer and assigns result to myVar1
function getNumberRand(aNum)
   {
     //return Math.floor(Math.random() * aNum); 
     myVar1 =  Math.floor(Math.random() * aNum); 
   }

   function findArrayparts(myArray1, myArray2)    
    {   
// calls getNumberRand function         
      getNumberRand(myArray1.length); 

      // works out characters at same index in both arrays
      myVar2 = (myArray1(myVar1));
      myVar3 = (myArray2(myVar1)); 

      // new array to hold output
      outputArray = new Array(myVar2, myVar3);

        // display output
      outputArray.join();
        alert(outputArray); 
    }    

// Calls findArrayparts function        
    findArrayparts(myArray1, myArray2);


</SCRIPT>


</HEAD>
<BODY>
</BODY>
</HTML>

Upvotes: 0

Views: 87

Answers (4)

daiscog
daiscog

Reputation: 12057

Why are you using the global variables myVar1, myVar2, myVar3? Use return to get values out of a function.

Is this what you're trying to achieve:

<script type="text/javascript">

var myArray1 = ['red', 'orange', 'green', 'blue', 'yellow'];
var myArray2 = ['tomato', 'orange', 'apples', 'blueberry', 'banana'];

function getNumberRand(aNum)
{
    return Math.floor(Math.random() * aNum); 
}

function findArrayparts(myArray1, myArray2)    
{   
    var index = getNumberRand(myArray1.length); 
    alert(outputArraymyArray1[index] + ', ' + myArray2[index]);
}    

findArrayparts(myArray1, myArray2);


</script> 

Upvotes: 0

user113716
user113716

Reputation: 322462

DEMO: http://jsfiddle.net/PjtxY/

var myArray1 = ['red', 'orange', 'green', 'blue', 'yellow'];
var myArray2 = ['tomatoe', 'orange', 'apples', 'blueberry', 'banana'];


function getNumberRand(aNum) {
    return Math.floor(Math.random() * aNum); 
}

function findArrayparts(myArray1, myArray2) {

        // Store return value in local variable   
    var random_num = getNumberRand(myArray1.length);

       // Use [] square brackets to get array member
    var myVar2 = myArray1[ random_num ];
    var myVar3 = myArray2[ random_num ];

       // Store new Array in local variable.
    var outputArray = new Array(myVar2, myVar3);

       // Store joined result in a variable
    var result = outputArray.join();

       // Alert the result
    alert( result );
}

findArrayparts(myArray1, myArray2);

Upvotes: 2

Jamiec
Jamiec

Reputation: 136074

You had getNumberRand right before you commented it out. You want to return the random number:

function getNumberRand(aNum)
{
 return Math.floor(Math.random() * aNum); 

}

Then when you come to want to use the return from that function later on you just assign the result to a variable:

function findArrayparts(myArray1, myArray2)    
{   
// calls getNumberRand function         
  var myVar1 = getNumberRand(myArray1.length); 

  // works out characters at same index in both arrays
  myVar2 = (myArray1(myVar1));
  myVar3 = (myArray2(myVar1)); 

  ...
}

Upvotes: 0

FishBasketGordo
FishBasketGordo

Reputation: 23132

You had it in the commented out line. Just return the value:

function getNumberRand(aNum)
{
    return Math.floor(Math.random() * aNum); 
}

Upvotes: 0

Related Questions