d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Debugging Logic Errors > Tips?
Add Reply New Topic New Poll
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Oct 18 2016 11:35am
I have an assignment to debug this js program that the instructor assigned. I have fixed all the errors except the logic errors. What are some useful ways to find logic errors in code?
Member
Posts: 17,090
Joined: Nov 22 2008
Gold: 169.00
Oct 18 2016 12:40pm
They are called logical part of program for a reason. You need think logically, understand and follow your code.

Sometimes when I get some errors in loops I make random printf statement inside them just to see how long have my code has passed and where it got broken.
In your case I think it is window.alert("sometext"); or something in JS, just insert couple of it in loops to see where it crashes.

Or you can post the problem here, and we go can trought it together, and I could try explain logic how I noticed it.
Even so, I have never did anything big in JS, so if it is some syntax error, someone will notice it faster than me :)

This post was edited by OnlyInMemories on Oct 18 2016 12:41pm
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Oct 18 2016 04:12pm
Write the following above whatever is confusing you in your js:
debugger;

Then run your code and it should start the debugger in whatever browser you're using. Just step through your code from there.

Or put a bunch of console.log() statements in and print what you need to know.
Member
Posts: 16,404
Joined: Mar 28 2009
Gold: 7.69
Oct 18 2016 07:05pm
Quote (OnlyInMemories @ Oct 18 2016 01:40pm)
They are called logical part of program for a reason. You need think logically, understand and follow your code.

Sometimes when I get some errors in loops I make random printf statement inside them just to see how long have my code has passed and where it got broken.
In your case I think it is window.alert("sometext"); or something in JS, just insert couple of it in loops to see where it crashes.

Or you can post the problem here, and we go can trought it together, and I could try explain logic how I noticed it.
Even so, I have never did anything big in JS, so if it is some syntax error, someone will notice it faster than me :)


What i fixed was:

- added a break; statement to one of the cases because it was missing
-in the var answer function i changed the second var answer to "answerq' because var answer was redefining var answer = function().. thus adding a 'q' to each case
- deleted an extra }; at the very end of the code
-in the mousePressed function, I defined var questionCount; because the questionCount's were not being stored anywhere (im kind of skeptical on this one)
-added a few semicolons to where they were missing
-added '===' to one of the else/if statements. Before it was only '==' which is not 'equal to'

Code
//ProgramCodeGoesHere

var q = "Do you like the outdoors?";
var q1 = "Do you like to run?";
var q2 = "Do you like cheese?";
var q3 = "Do you like to swim?";

var yesButton = function () {
fill(255,255,255);
rect(40, 127, 50, 30);
fill(0,0,0);
textSize(18);
text("YES", 50,150);
};

var noButton = function(){
fill(255,255,255);
rect(290, 127, 50, 30);
fill(0,0,0);
textSize(18);
text("NO", 300, 150);
};

var question = function(){
fill(0,0,0);
textSize(18);
text(q, 100, 75, 300, 150);
};

var answer = function (a){
fill(255, 0, 0);
textSize(18);
var answerq = "";
switch (a) {
case 1:
answerq = "You are a horse!";
break;
case 2:
answerq = "You are a turtle!";
break;
case 3:
answerq = "You are a mouse!";
break;
case 4:
answerq = "You are a fish!";
break;
case 5:
answerq = "You are a cat!";
break;
}
text(answer, 150, 200);
noLoop();
};

draw = function (){
background(255,255,255);
fill(255,0,0);
textSize(24);
text("What animal are you?", 100, 30);
question();
yesButton();
noButton();
};

mousePressed = function(){
var questionCount;
if(mouseX >=40 && mouseX <= 90 && mouseY >= 127 && mouseY <= 157){ //yes
if (questionCount === 0 ){
q = q1;
questionCount = 1;
}
else if(questionCount === 1){
answer (0);
}
else if (questionCount === 2){
answer(3);
}
else if (questionCount === 2){
answer(4);
}
}
else if (mouseX >= 290 && mouseX <= 340 && mouseY >= 127 && mouseY <= 157){//no
if (questionCount === 0){
questionCount = 2;
q = q2;
}
else if (questionCount === 1){
answer();
}
else if (questionCount === 1){
q = q3;
questionCount = 3;
}
}
else if (questionCount === 3){
answer(4);
}
};


This post was edited by Shakti on Oct 18 2016 07:06pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 19 2016 05:54pm
one of the best things I was told when learning was to draw things out. Pen and paper.


Helps out a shit load
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Oct 19 2016 06:05pm
== IS equal to.
=== is equal to AND same type
Also, you should have a default in your switch statement. Good job figuring it out though.

I agree with Eep. Drawing it out or writing out what you want the thing to do really can help.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll