每当我运行此代码时,浏览器就会崩溃。我是在犯一个愚蠢的错误,还是我的代码存在严重错误?
我已经尽力尝试调试我的代码,但是当通过调试器进行调试时,我的代码显然没有错。怎么了?非常感激你的帮助。



function FrogFunction() {
  var yedec;
  var decidertwo;
  var decfrog;
  var ChordProgression = ["C Minor"];
  var decider = Math.random();
  var CurrentChord;
  if (decider <= 0.25) {
    var CurrentChord = "D Minor";
  } else if (decider > 0.25 && decider <= 0.5) {
    var CurrentChord = "E♭ Major";
  } else if (decider > 0.5 && decider <= 0.75) {
    var CurrentChord = "F Minor";
  } else if (decider > 0.75 && decider <= 1) {
    var CurrentChord = "A♭ Major";
  }
  ChordProgression.push(CurrentChord);
  while (CurrentChord != "B♭ Major" || CurrentChord != "G Major") {


    //if dminor
    if (CurrentChord = "D Minor") {
      decidertwo = Math.random();
      if (decidertwo <= 0.5) {
        CurrentChord = "G Major";
      } else {
        CurrentChord = "B♭ Major";
      }

    }


    //if dminor

    //if e flat major
    else if (CurrentChord = "E♭ Major") {
      yedecider = Math.random();
      if (yedecider <= 0.3) {
        CurrentChord = "F Minor";
      } else if (yedecider <= 0.6 && yedecider > 0.3) {
        CurrentChord = "G Major";
      } else {
        CurrentChord = "A♭ Major";
      }
    }
    //if e flat major


    //if f minor
    else if (CurrentChord = "F Minor") {
      decfrog = Math.random();
      if (decfrog <= 0.5) {
        CurrentChord = "G Major";
      } else {
        CurrentChord = "B♭ Major";
      }
    }
    //if f minor

    //if a flat major
    else if (CurrentChord = "A♭ Major") {
      yedec = Math.random();
      if (yedec <= 0.25) {
        CurrentChord = "E♭ Major";
      } else if (yedec <= 0.5 && yedecider > 0.25) {
        CurrentChord = "F Minor";
      } else if (yedec <= 0.75 && yedecider > 0.5) {
        CurrentChord = "G Major";
      } else {
        CurrentChord = "B♭ Major"
      }
    }
    //if a flat major


    ChordProgression.push(CurrentChord);
    //past this bracket is the end bracket of the while loop
  }

  document.getElementById("PlaceForChords").innerHTML = ChordProgression;
}

<center>
  <p></p>
  <p><button onclick="FrogFunction()">Generate Chords</button></p>
  <p id="PlaceForChords"></p>
</center>

最佳答案

该语句永远不会是false

while(CurrentChord!="B♭ Major" || CurrentChord!="G Major"){


CurrentChord不能同时是"B♭ Major""G Major"。永远不是那两个的其中之一。

要解决此问题,请将||替换为&&

“虽然和弦不是“ B♭大调”而和弦不是“ G大调” =>“做事”。

另外,preferably使用!==代替!=

因此,已修复:

while(CurrentChord !== "B♭ Major" && CurrentChord !== "G Major"){


要么:

while(!(CurrentChord === "B♭ Major" || CurrentChord === "G Major")){


从逻辑上讲,它们将返回相同的值。您使用的只是个人喜好。

关于javascript - 我在Java语言中无休止地循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46002509/

10-11 13:52