Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Unknown problem with my code? (Beginner)

1. 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))

ax2 = ax2 / multiplier;
bx = bx / multiplier;
c = c / mulitplier;
//divides question vars by gcf

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;
}
}
}

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. 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. 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";`

4. 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).

5. Originally Posted by sbhmf
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.

Originally Posted by sbhmf
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. [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.

Originally Posted by Linux_Sage
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. Originally Posted by Linux_Sage
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. 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```

9. 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. Originally Posted by sbhmf
...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. Originally Posted by Linux_Sage

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. 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
•