简体   繁体   English

for循环中的if语句将无法运行,我不确定为什么

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

A brief run-down - I'm using the wad JavaScript library to create a random tone generator. 简要介绍-我正在使用wad JavaScript库创建随机音调生成器。 I am trying to have the generator 'slide' between pitches, as follows in pseudocode: 我正在尝试使音高之间的生成器“滑动”,如伪代码中所示:

  1. Determine the current pitch (randomNum) 确定当前音高(randomNum)
  2. Determine the next pitch (randomNext) 确定下一个音高(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. 除第5步外,我一切正常。我试图使用一个for循环(在if / else语句中,根据下一个音高是更低还是更低)在每个音高上使用计数器进行迭代,但是根据我的记录在控制台语句中,for循环根本不会运行。

If I remove the for loop from the if statement and set its starter pitch manually from a variable, it runs as expected. 如果我从if语句中删除了for循环,并从一个变量中手动设置了它的启动音高,它将按预期运行。

What am I doing wrong? 我究竟做错了什么? Is there something wrong with my if statement? 我的if语句有问题吗? 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 . 并且我在这里已经完成了整个程序的JSFiddle。

Any assistance would be very much appreciated! 任何帮助将不胜感激!

The condition for that block is that randomNum < randomNext , but howManyCents is randomNum - randomNext , which will be negative in that case. 该块的条件是randomNum < randomNext ,但是howManyCentsrandomNum - randomNext ,在这种情况下为负。 The loop condition, then – i < howManyCents , with i starting at 0, will never be true. 然后,循环条件– i < howManyCentsi从0开始)将永远不成立。

You can use i < -howManyCents , or assign Math.abs(randomNum - randomNext) to howManyCents , or i > howManyCents and i-- . 您可以使用i < -howManyCents ,或者将Math.abs(randomNum - randomNext)分配给howManyCents ,或者将i > howManyCentsi--分配给。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM