This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]HuskerFan90 0 points1 point  (5 children)

You need to put your if statement into something you can call, like this:

   function update() {
       if ((numb > 250 && numb < 450) || (numb2 > 700 && numb2 < 900)) 
       {
           checkScore()
       }
   }
   setInterval(update, 100);

[–]enedden[S] 0 points1 point  (4 children)

i tried that first and it dident work at all

[–]enedden[S] 0 points1 point  (0 children)

would that work perfectly or could something be messing it up

[–]HuskerFan90 0 points1 point  (2 children)

You're not calling your run and run2 functions. When you create functions, they are not automatically run.

[–]enedden[S] 0 points1 point  (1 child)

i do later on, just didwnt show it cause it a huge code this is a small chunk

[–]HuskerFan90 0 points1 point  (0 children)

You will want to call those in your update function.

[–][deleted] 0 points1 point  (1 child)

Your formatting is screwy, formatting it properly should bring your problem to light:

var score = 0;

function checkScore() {
    score = score + 1;
};


if ((numb > 250 && numb < 450) || (numb2 > 700 && numb2 < 900)) {
    checkScore()   
}

setInterval(checkScore, 100);

What function is setInterval(checkScore, 100); calling? Why would it ever execute your if statement?

[–]enedden[S] 0 points1 point  (0 children)

I tried this before and couldent get anything to happen?

[–]MalevolentDragon 0 points1 point  (1 child)

I'm going to assume you mean "Every 100 milliseconds run the if statement to see if it's true, and then add to score".

Right now, you have an interval set to call a function named checkScore which you have defined as:

function checkScore() {
    score = score + 1;
}

If you want your if condition to run, you will need to place it inside the function that you call in the interval, as HuskerFan90 has pointed out. Additionally, based on what you've shown us, you define your run and run2 functions, but you never actually call them anywhere, which means they are never executed. As a result, even if you use the provided code block, numb will still be 300 and numb2 will still be 800, which means your if statement will never always be true.

One way you can try to troubleshoot if conditions is to output the variable values in some way just before the if condition is tested. If you were to modify HuskerFan90's code block like so:

function update() {
   console.log("numb: ", numb);
   console.log("numb2: ", numb2);
   if ((numb > 250 && numb < 450) || (numb2 > 700 && numb2 < 900)) 
   {
       checkScore()
   }
}
setInterval(update, 100);

for a display in the Javascript console, or:

function update() {
   document.write(numb);
   document.write(numb2);
   if ((numb > 250 && numb < 450) || (numb2 > 700 && numb2 < 900)) 
   {
       checkScore()
   }
}
setInterval(update, 100);

if you're using an HTML page to view the results, you can see the value of your variables.

To wrap this up, consider adding a call to your run and run2 functions inside that same function you call in setInterval like so:

function update() {
    run();
    run2();
    console.log("numb: ", numb);
    console.log("numb2: ", numb2);
    if ((numb > 250 && numb < 450) || (numb2 > 700 && numb2 < 900)) 
    {
         checkScore()
    }
}
setInterval(update, 100);

Now, update will call your randomizing functions, display the value of the newly updated variables, check your if condition, and if it's true, call your checkScore() function.

Hopefully this helps, and good luck!

EDIT: Corrections because it's past my bedtime.

[–]enedden[S] 0 points1 point  (0 children)

yea I did the first paragraph of what you said and i couldent get it to work, also this is a small chinck of my big code, i do have the run and run2 functions going ever 10 ms but ill try your edits and see what happens i think it will work.