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 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unknown problem with my code? (Beginner)

    Hey, I'm new to this forum and just started Javascript because my maths teacher challenged me to create a program to solve factoring questions. My code stops working at around the 20th line, and I don't know why. I've been using jsfiddle.net as my code compiler if that makes a difference.
    This is my code:

    Code:
    var ax2 = prompt('What is the ___x^2?');
    var bx = prompt('What is the ___x?');
    var c = prompt('What is the ___?');
    //get the original question vars
    
    /*-----\/BELOW IS NOT MY CODE!!\/-----*/
    function gcf(a, b) {
        if (b === 0) {
            return a;
        } else {
            return gcf(b, a % b);
        }
    }
    /*-----^^ABOVE IS NOT MY CODE!!^^-----*/
    
    //euclid's algorithm (finds gcf)
    
    var multiplier = gcf(gcf(ax2, bx), c);
    //get the gcf for later use (multiplier(num + num)(num + num))
    alert(multiplier);
    
    ax2 = ax2 / multiplier;
    bx = bx / multiplier;
    c = c / mulitplier;
    //divides question vars by gcf
    alert('We have divided by multiplier');
    
    var abx3 = ax2 * bx;
    
    for (i = 1; i++; i <= Math.sqrt(abx3)) { //start for loop
        if (abx3 % i === 0) { //if i goes evenly into abx3
            if (abx3 % i + i == bx) { //if the two possible factors equal bx
                var num1 = ax2;
                var num2 = abx3 % i;
                var num3 = ax2;
                var num4 = i;
            }
        }
    }
    alert('We have finished for loop!');
    
    var num12 = gcf(num1, num2);
    var num34 = gcf(num3, num4);
    
    alert('We have found GCF of the two numbers for each bracket!');
    
    //You really are awesome for reading this far!!
    
    num1 = num1 / num12;
    num2 = num2 / num12;
    num3 = num3 / num34;
    num4 = num4 / num34;
    alert('WE have divided each number by the respective gcf!');
    
    alert(multiplier + "(" + num1 + "x + " + num2 + ")(" + num3 + "x + " + num4);
    I use the alert() thing to let me know when the code has passed a certain point so I can pinpoint where the problem is. Unfortunately, I know where the problem is, I just don't know what it is.
    Thanks for taking the time to read this and, if you decide to help out, thanks for helping out!
    -Tim

    Sorry if this is in the wrong subforum, I didn't really understand what the different subforums were for, so I picked what I think is the most general one.

  • #2
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Well this particular problem has nothing to do with the DOM or JSON so yeah, wrong subforum. But anyway...

    Code:
    c = c / mulitplier;
    My console states that multiplier is undefined. May have something to do with this call:

    Code:
    var multiplier = gcf(gcf(ax2, bx), c);
    Math isn't my forte but something about that call doesn't look right. What exactly are you trying to do there? I mean it's already a recursive function....

    As a side note I recommend using Firebug or some other browser-based Web Development console to debug these Javascripts. They're typically in the Tools > Web Developer section of your browser.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,631
    Thanks
    0
    Thanked 648 Times in 638 Posts
    if you add the following statement at the top of your code then the console will tell you exactly which variable names you have mistyped:

    Code:
    "use strict";
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Jan 2013
    Location
    Sunnyvale, CA
    Posts
    98
    Thanks
    6
    Thanked 7 Times in 7 Posts
    For those who are not smarter than 5th graders (lol): Tim's code is designed to identify the greatest common factor of three numbers.

    The code is not sufficiently defensive. Sure, it tests for a zero denominator, which will crash the script, but imagine what might happen if someone entered 7 and 22... the recursive function would continue infinitely and cause a stack overflow.

    I wonder if that is what might be happening in your case..?

    Even if fast, recursion is memory-expensive, and the programmer MUST build into it a mechanism that ensures termination to avoid crashing the script.

    I haven't combed through your code (too late at night for it) and recommend that you break it down for testing: try running gcf(a, b) to see if it crashes, and if not then try gcf(gcf(a, b), c). Do the same with your squaring loop. It's often easier to discover the problem when the code is simpler.

    Also, may I suggest that your prompts can be more specific? The third prompt does not even imply that it is requesting a number(if you consider x and x^2 implications). While on the subject, you "might" want to insist that the user enters a natural number (i.e. > 0).
    Last edited by sbhmf; 04-06-2014 at 07:14 AM.

  • #5
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sbhmf View Post
    The code is not sufficiently defensive. Sure, it tests for a zero denominator, which will crash the script, but imagine what might happen if someone entered 7 and 22... the recursive function would continue infinitely and cause a stack overflow.
    When I try entering 7, 22, and 3 in it returns 0 as the GCF. It's a translation of a euclidean algorithm, so there shouldn't be an issue.

    [QUOTE=sbhmf;1392306]I haven't combed through your code (too late at night for it) and recommend that you break it down for testing: try running gcf(a, b) to see if it crashes, and if not then try gcf(gcf(a, b), c). Do the same with your squaring loop. It's often easier to discover the problem when the code is simpler.[\QUOTE]

    Alright, thanks I'll try doing that.

    Quote Originally Posted by sbhmf View Post
    Also, may I suggest that your prompts can be more specific? The third prompt does not even imply that it is requesting a number(if you consider x and x^2 implications). While on the subject, you "might" want to insist that the user enters a natural number (i.e. > 0).
    Right now it's really just designed for me. I'll make it more user-friendly once it actually works, until then there is no point in restating something that I already know if it's just for me.

  • #6
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [QUOTE=Linux_Sage;1392291]
    Code:
    c = c / mulitplier;
    My console states that multiplier is undefined. [\QUOTE]
    That was a spelling error, it was supposed to be multiplier, not mulitplier

    [QUOTE=Linux_Sage;1392291]
    Code:
    var multiplier = gcf(gcf(ax2, bx), c);
    Math isn't my forte but something about that call doesn't look right. What exactly are you trying to do there? I mean it's already a recursive function....[\QUOTE]

    That finds the GCF of three numbers, by finding the GCF of c and the GCF of a and b.

    Quote Originally Posted by Linux_Sage View Post
    As a side note I recommend using Firebug or some other browser-based Web Development console to debug these Javascripts. They're typically in the Tools > Web Developer section of your browser.
    I'll look into that, thank you.

  • #7
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Linux_Sage View Post
    Well this particular problem has nothing to do with the DOM or JSON so yeah, wrong subforum. But anyway...
    Where would be the correct spot to put this? Because I'm new to JS and this forum I don't really know what each forum name means.

  • #8
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    It should be in the main JavaScript forum. As it's related to core JS. One thing I've noticed is this loop of yours. It goes on forever. The incrementor is in the wrong place.. try:

    Code:
    for (i = 1; i <= Math.sqrt(abx3); i++) {
    I'd also convert your GCD function to use iteration instead of recursion such as in this pseudocode from the Wiki article:
    Code:
    function gcd(a, b)
        while b ≠ 0
           t := b
           b := a mod b
           a := t
        return a
    Last edited by Linux_Sage; 04-06-2014 at 03:48 PM. Reason: mispelling

  • #9
    New Coder
    Join Date
    Jan 2013
    Location
    Sunnyvale, CA
    Posts
    98
    Thanks
    6
    Thanked 7 Times in 7 Posts
    When I try entering 7, 22, and 3 in it returns 0 as the GCF. It's a translation of a euclidean algorithm, so there shouldn't be an issue.
    ...strange. 22/7 is pi, and its modulus goes on forever. Wouldn't one expect a stack overflow when modulus == 0 is the sentinel value that stops the recursion?

  • #10
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sbhmf View Post
    ...strange. 22/7 is pi, and its modulus goes on forever. Wouldn't one expect a stack overflow when modulus == 0 is the sentinel value that stops the recursion?
    There's actually some pretty awesome code there.
    Code:
    function gcf(a, b) {
        if (b === 0) {
            return a;
        } else {
            return gcf(b, a % b);
        }
    }
    Code:
    How it would run 7 and 22:
    gcf(7,22)
    if 22 = 0 return 7
    else gcf(22, 7 % 22) //7%22 is 7
    //begin again
    gcf(22, 7)
    if 7 = 0 return 22
    else gcf(7, 22%7) //22%7 is 1
    //begin again
    gcf(7,1)
    if 1 = 0 return 7
    else gcf(1, 7%1) //7%1 is 0
    //begin again
    gcf(1,0)
    if 0 = 0 return 1 //FINISHED!

  • #11
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Linux_Sage View Post

    I'd also convert your GCD function to use iteration instead of recursion
    What is the advatage of iterative vs recursive programming?

    Thanks for finding the error in the for loop!

  • #12
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Because there comes a point in some recursive solutions where you experience a stack overflow and your script blows up or it could take an agonizingly long time versus a simple iterative approach.
    If you want to go recursive you may want to take a more dynamic programming approach and optmize via memoization.

    Memoization - Wikipedia, the free encyclopedia


  •  

    Posting Permissions

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