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 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can someone tell me where I went wrong...?

    Well, I was trying to make this program for my PSP, and I started in Flash, then converted it to PHP, and finally I need to convert it to Javascript. All my program does it take a user-entered number, and give all the factors (multiplication) for it, in the form of a table (collective string). In PHP, it works fine, in fact here it is:
    http://www.sticktoons.net/ssjskipp/random/GCF.php

    But in Javascript (which I have no expierence with), it messed up...here's the script:

    Code:
    <form name="my_form">
    <p>
    <input name="num" type="text" id="num" value="0">
    <input type="button" name="button" value="Get the GCF"><p>
    </form>
    <script language="javascript">
        gcf = 1
        g = 1
        num = document.my_form.num
        a = num
        b = num
        i_max = num/2
        str = ""
        i = 0
        cur_fact = 0
        document.my_form.button.onclick ="get_gcf()"
        document.alert(document.my_form)
    function get_gcf(){
        num = document.my_form.num
        if (Number(num) == num){
            gcf = 1
            g = 1
            a = num
            b = num
            i_max = num/2
            str = ""
            i = 0
            cur_fact = 0
        }
        if (gcf == 1){
            document.write("<hr>Your Greatest Common Factors:<br/><table border=\"2\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>Factor</td><td>Factor</td><td>Added Together</td></tr>")
            while (g > 0){
                if (g == 1) {
                    cur_fact = (i*a)
                    if (cur_fact == num) {
                        document.write("<tr><td>".i."</td><td>".a."</td><td>".(i+a)."</td></tr>")
                    }
                    g = 2
                } else if (g == 2) {
                    if (a>1 && i<i_max) {
                        a--
                        g = 1
                    } else if (a<=1 && i<i_max) {
                        b--
                        i++
                        a = b
                        g = 1
                    } else if (i>=i_max) {
                        g = 0
                    }
                }
            }
            document.write("</table>")
            }
        }
    }
    </script>
    Can anyone help? (I have NO idea what in the pie I did wrong, BTW)

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    document.write("<tr><td>".i."</td><td>".a."</td><td>".(i+a)."</td></tr>")
    I do not know if this particular syntax is a holdover of something you do in PHP as I am not PHP literate, or just a misunderstanding as to how to use it, however if you replace the .'s with +'s it should be fine. The use of the period in javascript is to access a property of an object, objName.someProperty. Perhaps you thought you could access the cell this way. I did not check the functioning of the math as you already have demonstated you can do the math.

    BaldEagle

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...I'm a dumbass...thanks =]
    Darn it, still didn't work...is there like, an actual problem with the script? Like, the valus and such?

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you explain what is wrong. Are the numbers wrong, is the table layout messed up?

    BaldEagle

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry to bailout on you, but the wife is bugging me to get going as we have to go out for a bit. I'll try to get back to you later if no one else does.

    BaldEagle

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
       "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
      <head>
        <title>Test</title>
        <script type="text/javascript">
        <!--
        function get_gcf()
        {
          var num = document.my_form.num.value;
       
          if (isNaN(num))
          {
            alert("You must enter a valid number!");
          }
          else
          {
            var g = 1;
            var a = num;
            var b = num;
            var str = "";
            var i;
            var cur_fact = 0;
    
            str = '<hr>Your Greatest Common Factors:<br/><table border="2" cellspacing="0" cellpadding="0"><tr><td>Factor</td><td>Factor</td><td>Added Together</td></tr>';
            while (g > 0)
            {
              for (i=0; i<=b; i++)
              {
                cur_fact = i * a;
                if (cur_fact == num) 
                {
                  str += "<tr><td>"+i+"</td><td>"+a+"</td><td>"+(parseInt(i)+parseInt(a))+"</td></tr>";
                }
              }
              a--;
              if (a == 0)
              {
                g = 0;
              }
            }
            str += "</table>"
            document.getElementById("result").innerHTML = str;
          }
        }
        // -->
        </script>
      </head>
    
       <body text=#000000 bgColor=#ffffff>
         <form name="my_form">
           <p>
             <input name="num" type="text" id="num" value="0">
             <input type="button" name="button" value="Get the GCF" onclick="get_gcf();">
           <p>
         </form>
         <div id="result"></div>
       </body>
    </html>
    I'm back now. Before I logged back in I wrote this up. This will do what you want, but keep in mind that higher numbers will not be handled quickly like lower number are. Javascript is not the fastest running code simply because it is interpretted at run time. Also I have used the longest form of the syntax for clarity but there is nothing wrong with your coding method. Semi-colon's are not required either it is just my habit. As for your original code I played with it a little and couldn't get it to work completely and changing variables would get me close but not all the way. So if you want to go back to your code and make it work let me know and I'll try to accomodate you. This doesn't run all that much faster but is certainly much shorter.

    BaldEagle

  • #7
    New Coder
    Join Date
    Jan 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thankies ^_^, where, may I ask, did I go wrong (javascript wise)?

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ssjskipp
    Code:
    <form name="my_form">
    <p>
    <input name="num" type="text" id="num" value="0">
    <input type="button" name="button" value="Get the GCF"><p>
    </form>
    <script language="javascript">
        gcf = 1
        g = 1
        num = document.my_form.num
        a = num
        b = num
        i_max = num/2
        str = ""
        i = 0
        cur_fact = 0
        document.my_form.button.onclick ="get_gcf()"
        document.alert(document.my_form)
    function get_gcf(){
        num = document.my_form.num
        if (Number(num) == num){
            gcf = 1
            g = 1
            a = num
            b = num
            i_max = num/2
            str = ""
            i = 0
            cur_fact = 0
        }
        if (gcf == 1){
            document.write("<hr>Your Greatest Common Factors:<br/><table border=\"2\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>Factor</td><td>Factor</td><td>Added Together</td></tr>")
            while (g > 0){
                if (g == 1) {
                    cur_fact = (i*a)
                    if (cur_fact == num) {
                        document.write("<tr><td>".i."</td><td>".a."</td><td>".(i+a)."</td></tr>")
                    }
                    g = 2
                } else if (g == 2) {
    // from this point down is where I was having trouble getting the full
    // table to show.  I entered 6 and got a table with 
    // 1     6     7
    // 2     3     5
    // so I changed the i_max to b and got this
    // 1     6     7
    // 2     3     5
    // 3     2     5
    // at that point I sort of said to myself there must be a better
    // way so as not to have to keep track of all those vars.
    // I suspect that if you play around with this part of the
    // code you will eventually resolve the problem in your
    // original code.     
                    if (a>1 && i<i_max) {
                        a--
                        g = 1
                    } else if (a<=1 && i<i_max) {
                        b--
                        i++
                        a = b
                        g = 1
    // here seems to be the issue because if we never allow
    // i to be > i_max then how will we display an i value greater
    // than half of the num?
                    } else if (i>=i_max) {
                        g = 0
                    }
                }
            }
            document.write("</table>")
            }
        }
    }
    </script>

    BaldEagle

  • #9
    New Coder
    Join Date
    Jan 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, thankies ^_^


  •  

    Posting Permissions

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