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 the开发者_如何学Gon 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>
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);
You had it in the commented out line. Just return the value:
function getNumberRand(aNum)
{
return Math.floor(Math.random() * aNum);
}
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));
...
}
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>
精彩评论