Results 1 to 3 of 3

Thread: Conceptually, how would I do this in javascript:

  1. #1
    Join Date
    Oct 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Conceptually, how would I do this in javascript:

    After the user inputs a set of data, I need to output the mean, median, and mode; I tell them to input the numbers, with each number separated by a q, so I can easily use arrays. MAke sure the array is parse'd.

    Mean - split the array at the separation character. Create a loop, where I set a variable equal to the current case of the array, added with the sum of all the previous cases of the array. When it gets to the end, take the sum of all the previous cases of the array and the last case of the array, and divide that by the length of the array.

    Mode - split the array at the separation character. Sort the resulting numbers from lowest to highest. Honestly, I have no idea how I would economically figure out the most occuring number in my array.

    Median - take the length of the array, and if the module of the array is 0, display the number (after its been sorted) that is at the index of the length of the array divided by 2. If the module is 1, take the average of half the index of the length of the array +1, and the index of the array -1.

    document.write the number out.

    Can anyone point me in the right direction as to how I could flesh these thoughts out into code, or if my thoughts are totally wrong, and help me think in javascript better?

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Your plan seems sound enough(I rarely think things out ahead of time. I set goals of what I want something to accomplish and worry about one small piece at a time). Do yu know javascript? If not, then that is where to start. What you are talking about is not too hard, so a good online tutorial would be good.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Sounds fine to me as well.

    Math in JS is very basic syntax... just look up a tutorial, and that'll get you started.

    document.write is perhaps the hardest function in that

    For your project, logic is the hard thing, and you seem to have that all worked out... now just go type out some code.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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