...

View Full Version : Javascript Function with Array



Clueless109
09-19-2011, 12:30 AM
Hello,

I'm busy trying to get a piece of code to work, for a homework assignment and ive looked at it for over 3 hours and can not see where im going wrong. Im pretty new to Java, so bear with me :confused:

Please see below question asked along with what I have got so far.

The function should begin by calling the getRandomNumber() function with the length of the argument arrays as the argument. The elements of the two argument arrays at the index of the random number are then returned as an array.

Complete the function according to the description above and the specification provided by the function comment.

Paste your getArrayElements() function, including code comments, into your Solution Document.


(b)

We have provided two parallel arrays for you to test the
getArrayElements() function. These have been assigned to the
variables termArray and definitionArray.
Edit testForm’s randomEntryButton ONCLICK event handler so
that getArrayElements() is invoked with the variables
termArray and definitionArray as arguments and the returned
array is displayed in an alert dialogue.
Run the program and carry out a test by clicking the button labelled ‘Get
Array Elements’. You should see an alert dialogue box similar to
Figure 3. Notice that the text representation of an array separates each
element from the next with a comma.
Paste your completed ONCLICK event handler into your Solution

Document.


// array of terms
var termArray = ['hard disk', 'magnetic tape', 'flash drive', 'optical disc', 'ram'];

// array of definitions
var definitionArray = ['Term 1', 'Term 2', 'Term 3', 'Term 4', 'Term 5'];


function getArrayElements(aString, aDelimiter)
{
var stringArray = aString.split(definitionArray);
var result = getRandomNumber(stringArray.length);
return result;
}
var termString = 'hard disk*Term 1#magnetic tape*Term 2#flash drive*Term 3#optical disc*Term 4#ram*Term 5';

Thanks very much in advance for your help :)

Old Pedant
09-19-2011, 12:49 AM
I see no place in the problem definition where it specifies that you are to use a delimited string (your termString thingy...whatever it is for).

So far as I can see, your getArrayElements function can't possibly be right. It returns nothing but a single number, not at all what was asked for.

Where did you get it from?

Clueless109
09-19-2011, 01:04 AM
Thanks for your reply.

The single number refers to the random number to return for the array. This was :

function getRandomNumber(aNumber)
{
// Picking a random number and rounding to down to whole number
var aNumber = Math.floor(Math.random()*51);
// Producing the number generated
return aNumber;
}

The part I think im struggling with is the array, but not sure how to fix it :S

minkoko
09-19-2011, 01:06 AM
function getArrayElements(aString, aDelimiter)

can explain me about the aString,aDelimiter in function

How it work ,

Thanks advance

Clueless109
09-19-2011, 01:17 AM
I think im completely lost :mad:

Old Pedant
09-19-2011, 02:51 AM
The single number refers to the random number to return for the array.
Yes, but then all your getArrayElements function does is return THAT VERY SAME NUMBER. It does *NOT* satisfy the requirements of the function, which are

The elements of the two argument arrays at the index of the random number are then returned as an array.

On top of that your onclick handler is supposed to work

so
that getArrayElements() is invoked with the variables
termArray and definitionArray

You might *start* with that.

You never do that.

But you might also want to write your getArrayElements function to satisfy the requirements of the assignment.

Old Pedant
09-19-2011, 02:53 AM
function getArrayElements(aString, aDelimiter)

can explain me about the aString,aDelimiter in function


Well, in his code, it doesn't work. He completely ignores those arguments. He needs to use them in completing the requirements of the function.

But as for your question: What part of passing arguments to a function do you not understand?

blaze4218
09-19-2011, 07:58 AM
First of all your function starts on the wrong foot. The assignment asks that "the function should begin by calling the getRandomNumber() function with the length of the argument arrays as the argument." But you have started by trying to split the first array using the second array as a delimiter... let's clear that up a bit: You don't split an array, you split a string to make an array. When you call .split() to split a string you must provide a character sequence (or in most cases a single character) to tell the method what to split the string at.
Example



<script>

myFirstArray = ['Term 1', 'Term 2', 'Term 3', 'Term 4', 'Term 5'];
myFirstString = myFirstArray.toString();

</script>

<button onclick="getElementById('Debugger').innerHTML=myFirstString;">Show my String</button>

<div id="Debugger"></div>



In that script I had an array that I changed into a string so that you can see the "delimiter character" that separates each array element. When you click on the button in that HTML page you will see the following output Term 1,Term 2,Term 3,Term 4,Term 5 . This is a visual representation of your string. The delimiter in this example is the comma character ","

//Another example coud be the string Term 1@Term 2@Term 3@Term 4@Term 5 and the delimeter would be "@" (get it?)

So to split the string value I created in my first example I would use the comma character as my delimeter in the call to .split()




<script>

myFirstString = myFirstArray.toString();
myNewArray = myFirstString.split(",");

</script>



I would NOT use the original array as my delimeter




<script>

myFirstArray = ['Term 1', 'Term 2', 'Term 3', 'Term 4', 'Term 5']
myFirstString = myFirstArray.toString();

//This is wrong
myNewArray = myFirstString.split(myFirstArray);

</script>



When done as in the first "split()" example you have now correctly split a string into an arry

You stated you needed the array thing cleared up so I hope that helps. But your task is not to edit your array in any way... So back to your homework...

Your first statement in your function getArrayElements() is supposed to send the length values of both arrays to the getRandomNumber() function, what you decided to do was split an array (incorrectly) then store that value as "stringArray" followed by calling getRandomNumber() with the length of the new array as your argument.
So the first thing to do is create your function getArrayElements() with a statement that calls getRandomNumber() with 2 arguments. Those arguments are the lengths of your 2 arrays. That new function needs to choose 2 random numbers no less than 0 but no greater than the length of the arrays (hint: the arguments you sent).

I find that it helps to write out the problem in the above fashon to really understand what you want to code. That should be a good starting point for you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum