Results 1 to 4 of 4
  1. #1
    xquzive is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Lettings a user add new values to an array.

    Hello guys,

    I'm very new to javascript and I have a question.


    I'm creating a calculator. This calculator has a memory function in it. When people inserted a value in the calculator then the user can save this value with button "M+". When the user wants to use this value again he can simply paste it into the calculator with button "MR".

    This works fine, however I'm working on a function that will remember the different memory value when a user clicks on the button. So if the memorized value is 5, and the user will click the button "ATA" (add to array) it will create the value "5" into an array (which will be the value of array[0]. When the user remembers a other memory value, lets say 9, and clicks on the button "ATA" again it will create the value "9" into the same array (which will be the value of array[1]).

    My current method doesn't seem to work, it only saves the first value. What am I doing wrong here?

    Java Code:
    // ATA
    function saveAta() {
    	var saved = new Array();
    	saved.push(m); // variable m is from the function of M+
    }

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    First you we need more clarification about what your are doing. Second in you saveAta() you have to

    //create a new array of length 1 + the length of the previous array
    // previous array = new array
    this will let you increase the size of you array after each addition of a new value

    I think this is the solution for your problem if i got it correctly

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I didn't read your post but I saw you say you are new to JavaScript and would like to point out java is not JavaScript.

    Java is to JavaScript as ham is to hamster.

    You would be better suited to go to a JavaScript dedicated site for advice on it.

    If you meant java, then disregard this but make sure you understand there is a difference.

    To actually give you an answer of the logic. You need to increase the array by 1 each time, copy the old contents to the new array, add the item to the end of the new array and then assign old array to the new array as pointed out above.
    Last edited by sunde887; 04-09-2011 at 01:01 PM.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    What am I doing wrong here?
    The question should be "What am I doing here". This is a Java-forum.
    Java relates to Javascript as Shranan Tongo to Swahili.

    In one way you are blessed making a calculator in Javascript: people here are sometimes directed to javascript for the eval-function (but better make your own code):
    I need to execute a formula taken from a db at runtime How-To

    Sorry Sunde, you were faster.
    Last edited by Jodokus; 04-09-2011 at 12:56 PM.

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  2. Comparing values in the table with the value the user keyed in
    By pancakeleh in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-06-2011, 02:31 AM
  3. Replies: 5
    Last Post: 11-22-2010, 08:33 PM
  4. draw shape with user defined values
    By cka in forum New To Java
    Replies: 4
    Last Post: 07-14-2010, 04:35 PM
  5. Averages of user inputed values (Need Help)
    By Zebra in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:51 PM

Tags for this Thread

Posting Permissions

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