For loop inside if statement will not run and I'm not sure why

160 views Asked by At

A brief run-down - I'm using the wad JavaScript library to create a random tone generator. I am trying to have the generator 'slide' between pitches, as follows in pseudocode:

  1. Determine the current pitch (randomNum)
  2. Determine the next pitch (randomNext)
  3. Determine whether the next pitch is higher or lower
  4. Play the current pitch and hold it for a second
  5. Play all the pitches between the current pitch and the next pitch quickly to give a 'sliding' effect - this is the part that isn't working!
  6. Play the next pitch and hold it for a second (and then reassign it as the current pitch).

I have got everything working except for step 5. I am trying to use a for loop (within an if/else statement according to whether the next pitch is higher or lower) to iterate over each pitch using a counter, but according to my logged console statements, the for loop never runs at all.

If I remove the for loop from the if statement and set its starter pitch manually from a variable, it runs as expected.

What am I doing wrong? Is there something wrong with my if statement? With my for loop?

The problematic bit of code is this:

// play the first pitch    
function playFirst() {
            if(!stopped){
                window.randomNum = Math.round(Math.random()*200 + 100);
                console.log("randomnum is now "+ randomNum);
            }
            playRandom();
        }

// play and loop subsequent pitches
        function playRandom(){
            if(!stopped){

                var randomNext = Math.round(Math.random()*200 + 100);
                console.log("randomNext is now " + randomNext);

                var howManyCents = randomNum - randomNext;
                console.log(howManyCents + " cents");

// ascending note slide condition
                if (randomNum < randomNext) {
                    console.log("randomnum is less!");

                    var inbetweenNum = randomNum + 1;

// for loop - the part with the problem! 
                    for (var i = 0; i < howManyCents; i++) {
                        inbetweenNum = randomNum + i;
                        console.log("inbetween number is " + inbetweenNum); 
                        inbetween.play({ pitch : inbetweenNum });
                        console.log("played inbetween up"); 
                    }
// descending note slide condition
                } else {
                    console.log("randomnum is more!");

                    var inbetweenNum = randomNum - 1;

// another problematic for loop
                    for (var i = 0; i > howManyCents; i--) {
                        inbetweenNum = randomNum - i;
                        console.log("inbetween number is " + inbetweenNum); 
                        inbetween.play({ pitch : inbetweenNum });
                        console.log("played inbetween down");
                    }
                } 
// actually play the note
                bell.play({ pitch : randomNext, wait: 0 });
                console.log("played randomnext" + randomNext);

// reassign the new note as the current note
                randomNum = randomNext;
                console.log("randomnum is now" + randomNum);

                setTimeout(playRandom,1500); // and loop it
            }
        }

and I have made a JSFiddle of the full program here.

Any assistance would be very much appreciated!

1

There are 1 answers

1
Ry- On BEST ANSWER

The condition for that block is that randomNum < randomNext, but howManyCents is randomNum - randomNext, which will be negative in that case. The loop condition, then – i < howManyCents, with i starting at 0, will never be true.

You can use i < -howManyCents, or assign Math.abs(randomNum - randomNext) to howManyCents, or i > howManyCents and i--.