SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Arrays help

  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrays help

    Not sure why I all the Months shows up for each Sales in the text area instead of individually like January: 46987, February: 43768, etc. And my High and Low does not show the Months either, only numbers. Please see img. Thanks for the help.



    Here are the codes:
    Code JavaScript:
    <html>
      <head>
      <title></title>
     
      <script type="text/javascript">
        function Main()
        {
        var Sales=new Array(12);
        var Average;
        var Low,High;
     
        Months=["January","February","March,April","May","June","July","August","September","October","November","December"];
        Sales=[46987,43768,38987,36181,35129,40245,52156,49546,51120,47345,45675,53769];
     
        //input section
     
     
     
        //calculate section
        Average=CalcAverage(Sales);
        High=CalcHigh(Sales);
        Low=CalcLow(Sales);
     
        //output section
        for (J=0;J<12;J++)
        {
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+Months+" " +Sales[J]+"\n";
        }
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nAverage: "+Average+"\n";
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nHigh: "+High+"\n";
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"Low: "+Low;
        }
     
        function CalcAverage(Sales)
        {
        var Total,Average;
        var J;
     
        Total=0;
        for (J=0;J<12;J++)    
        {
        Total+=Sales[J];
        }
        Average=Total/12.;
     
        return Average;
        }
     
        function CalcHigh(Sales)
        {
        var High,J;
     
        High=Sales[0];
        for (J=0;J<12;J++)
        {
        if (Sales[J]>High)
        High=Sales[J];
        }
     
        return High;
        }
     
        function CalcLow(Sales)
        {
        var Low,J;
     
        Low=Sales[0];
        for(J=0;J<12;J++)
        {
        if (Sales[J]<Low)
        Low=Sales[J];
        }
     
        return Low;
        }
      </script>
      </head>
     
      <body bgcolor="lightgray" style="color:darkblue">
     
     
        <form name="frmMain" style="text-align:center">
        <br />
     
        January <input type="text" name="txtSales1" size=4 value=46987><br /><br />  
        February <input type="text" name="txtSales2" size=4 value=43768><br /><br />   
        March <input type="text" name="txtSales3" size=4 value=38987><br /><br /> 
        April <input type="text" name="txtSales4" size=4 value=36181><br /><br />  
        May <input type="text" name="txtSales5" size=4 value=35129><br /><br />  
        June <input type="text" name="txtSales6" size=4 value=40245><br /><br />   
        July <input type="text" name="txtSales7" size=4 value=52156><br /><br />  
        August <input type="text" name="txtSales8" size=4 value=49546><br /><br />  
        September <input type="text" name="txtSales9" size=4 value=51120><br /><br /> 
        October <input type="text" name="txtSales10" size=4 value=47345><br /><br />  
        November <input type="text" name="txtSales11" size=4 value=45675><br /><br />  
        December <input type="text" name="txtSales12" size=4 value=53769><br /><br />
     
        <br /><br /><br />
        <input type="button" value="Calculate Average" onClick="Main();"/>
        <br /><br /><br />
        <textarea name="taOutput" rows=14 cols=30 wrap="virtual"></textarea>
     
      </form>
      </body>
    </html>

  2. #2
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In the output section you have +Months+, instead of +Months[J]+ . For it to work properly you need to rewrite the output section like this:

    //output section
    for (J=0;J<12;J++){ document.frmMain.taOutput.value=document.frmMain.taOutput.value+Months[J]+" " +Sales[J]+"\n"; }
    //
    document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nAverage: "+Average+"\n";
    document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nHigh: "+High+"\n";
    document.frmMain.taOutput.value=document.frmMain.taOutput.value+"Low: "+Low
    }
    // -----------

    Notice also that the last three lines are not in the loop. You only need these outputs once.

    One other thing, you don't need to keep writing document.frmMain.taOutput. It will be much easier to read if you use a shortcut like:
    var dOut=document.frmMain.taOutput, and then use dOut instead of the long form.

  3. #3
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GRRR!!! My High and Low was working, but when I put it in as a Job3, I get "undefined." Same with the Quarter Sales, I also get "undefined."

    Code JavaScript:
    <html>
      <head>
     
      <!-- Class: CIS 1207 Programming Logic & Design-->
      <!-- Assignment: Assignment Program #8: Arrays-->
     
      <title>CIS 1207 Assignment Program #8: Arrays</title>
     
      <script type="text/javascript">
        function Main()
        {
        var Sales=new Array(12);
        var Months=new Array(12);
        var Total,Average;
        var Q1Total,Q1Average,Q2Total,Q2Average,Q3Total,Q3Average,Q4Total,Q4Average;
        var Low,High;
     
        Months=["January","February","March","April","May","June","July","August","September","October","November","December"];
        Sales=[46987,43768,38987,36181,35129,40245,52156,49546,51120,47345,45675,53769];
     
     
        //input section
        Choice=parseFloat(document.frmMain.txtChoice.vlaue);
     
        Choice=Job1(Total,Average);
     
        Total=0;
     
        for (J=0;J<12;J++)
        {
        Total+=Sales[J];
        }
        Average=Total/12;
     
        if (Choice==1)
        {
        Job1(Total,Average);
        }
     
        else if (Choice==2)
        {
        Job2(Quarter);
        }
        else if (Choice==3)
        {
        Job3(High,Low);
        }
     
        //output section
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nTotal Sales: "+Total+"\n";
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nAverage Sales: "+Average+"\n";
     
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nQuarter1 Total: "+Q1Total;
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nQuarter1 Average: "+Q1Average+"\n";
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nQuarter2 Total: "+Q2Total;
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nQuarter2 Average: "+Q2Average+"\n";
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nQuarter3 Total: "+Q3Total;
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nQuarter3 Average: "+Q3Average+"\n";
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nQuarter4 Total: "+Q4Total;
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nQuarter4 Average: "+Q4Average+"\n";
     
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"\nHigh: "+High+"\n";
        document.frmMain.taOutput.value=document.frmMain.taOutput.value+"Low: "+Low;
     
        //calculate section
        function Job1(Total,Average)
        {
        var Total,Average;
        var J;
     
        Total=0;
        for (J=0;J<12;J++)    
        {
        Total+=Sales[J];
     
        return Total;
        }
     
        Total=0;
        for (J=0;J<12;J++)    
        {
        Total+=Sales[J];
        }
        Average=Total/12;
     
        return Average;
        }
     
        function Job2(Quarter)
        {
        var Q1Total,Q1Average,Q2Total,Q2Average,Q3Total,Q3Average,Q4Total,Q4Average;
        var J;
     
        Q1Total=0;
        for (J=0;J<3;J++)
        {
        Q1Total+=Sales[J];
        }
        Q1Average=Q1Total/3;
     
        Q2Total=0;
        for (J=3;J<6;J++)
        {
        Q2Total+=Sales[J];
        }
        Q2Average=Q2Total/3;
     
        Q3Total=0;
        for (J=6;J<9;J++)
        {
        Q3Total+=Sales[J];
        }
        Q3Average=Q3Total/3;
     
        Q4Total=0;
        for (J=9;J<12;J++)
        {
        Q4Total+=Sales[J];
        }
        Q4Average=Q4Total/3;
     
        return Quarter;
        }
     
        function Job3(High,Low)
        {
        var High,Low;
        var J;
     
        High=Sales[0];
        for (J=0;J<12;J++)
        {
        if (Sales[J]>High)
        High=Sales[J];
        }
     
        return High;
        }
     
        Low=Sales[0];
        for(J=0;J<12;J++)
        {
        if (Sales[J]<Low)
        Low=Sales[J];
        }
        return Low;
     
     
       } 
      </script>
      </head>
     
       <body bgcolor="lightgreen" style="color:darkblue">
       <h2 style="text-align:center"><u>Widget Company Sales Report</u></h2>
     
        <form name="frmMain" style="text-align:center">
        <br />
        <center><h4>Monthly Sales</h4>
        <table border="1">
        <tr><td>January</td><td>46987</td></tr>
        <tr><td>February</td><td>43768</td></tr>
        <tr><td>March</td><td>38987</td></tr>
        <tr><td>April</td><td>36181</td></tr>
        <tr><td>May</td><td>35129</td></tr>
        <tr><td>June</td><td>40245</td></tr>
        <tr><td>July</td><td>52156</td></tr>
        <tr><td>August</td><td>49546</td></tr>
        <tr><td>September</td><td>51120</td></tr>
        <tr><td>October</td><td>47345</td></tr>
        <tr><td>November</td><td>45675</td></tr>
        <tr><td>December</td><td>53769</td></tr>
        </table></center>
        <br />
        <center><table>
        <tr><td><u>Choices</u>:</td><td>
        <tr><td>1) Total and Average Monthly Sales</td></tr>
        <tr><td>2) Total and Average Sales for each Quarter</td></tr>
        <tr><td>3) List of months that sales exceed the average monthly sales</td></tr>
        <tr><td>4) Month with the greatest sales and month with lowest sales</td></tr>
        </center></table>
        <br />
     
        Enter Choice: <input type="text" name="txtChoice" size=5 value=""/>
     
        <br /><br /><br />
        <input type="button" value="Calculate Average" onClick="Main();"/>
        <br /><br /><br />
        <textarea name="taOutput" rows=25 cols=40 wrap="virtual"></textarea>
     
      </form>
      </body>
    </html>

  4. #4
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what it looks like.


  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,682
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    You may not be getting the right value from the form:

    Code:
    //input section
    Choice=parseFloat(document.frmMain.txtChoice.vlaue);
    With the next line, you are replacing that retrieved Choice value with what's returned from Job1.

    Code:
    Choice=Job1(Total,Average);
    I suspect that that line is a mistake and shouldn't be there at all.

    you don't need to pass Total or Average to Job1. If anything, it's the Sales array that you should pass to it, so that the function does not need to reply on accessing global information.

    Instead, I would have a function called total and another function called average, so that they can calculate their values from the array that's given to them.
    Here's how they might be used.

    Note: the variables here uses a lowercase initial letter, as an initial capital letter is normally reserved only for indicating an object constructor.

    Code javascript:
     
    var output = document.frmMain.taOutput;
    switch (choice) {
    case 1:
        output.value += "Total Sales: " + total(sales) + "\n";
        output.value += "Average Sales: " + average(sales) + "\n";
    case 2:
        q1 = [sales[0], sales[1], sales[2]];
        q2 = [sales[3], sales[4], sales[5]];
        q3 = [sales[6], sales[7], sales[8]];
        q4 = [sales[9], sales[10], sales[11]];
        output.value += "Quarter1 Total: " + total(q1) + "\n";
        output.value += "Quarter1 Average: " + average(q1) + "\n";
        ...
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With the next line, you are replacing that retrieved Choice value with what's returned from Job1.
    I suspect that that line is a mistake and shouldn't be there at all.
    you don't need to pass Total or Average to Job1. If anything, it's the Sales array that you should pass to it, so that the function does not need to reply on accessing global information.
    Well, what I'm trying to accomplish is the user select a Choice (1-4) and based the choices, the Jobs (1-4) are associated with it. But it's not working right, all the jobs show up regardless of what choice I chose.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,682
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    [QUOTE=jbinky;4862535 But it's not working right, all the jobs show up regardless of what choice I chose.[/QUOTE]

    The simplest way is:

    Code javascript:
    choice = parseInt(choice, 10);
    if (choice === 1) {
        ...
    } else if (choice === 2) {
        ...
    } else if (choice === 3) {
        ...
    } else if (choice === 4) {
        ...
    }
    Last edited by paul_wilkins; Apr 21, 2011 at 17:29.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do have the if and else if Choice in my code and parseFloat it.

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,682
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jbinky View Post
    I do have the if and else if Choice in my code and parseFloat it.
    So where has the value of Choice come from when the code gets to the if section?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •