Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Function with Array

    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

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Aug 2010
    Location
    myeik
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 5 Posts
    PHP Code:
    function getArrayElements(aStringaDelimiter
    can explain me about the aString,aDelimiter in function

    How it work ,

    Thanks advance

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think im completely lost

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by minkoko View Post
    PHP Code:
    function getArrayElements(aStringaDelimiter
    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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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
    Code:
    <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()

    Code:
    <script>
    
     myFirstString = myFirstArray.toString();
     myNewArray = myFirstString.split(",");
    
    </script>
    I would NOT use the original array as my delimeter

    Code:
    <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.
    Last edited by blaze4218; 09-19-2011 at 08:04 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •