0

Whenever I run this code, the browser crashes. Am I making a stupid mistake, or is there something seriously wrong with my code? I have tried as much as possible to debug my code, but when put through debuggers, there is apparently nothing wrong with my code. WHAT IS HAPPENING? Your help is very much appreciated.

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>
Durga
  • 15,263
  • 2
  • 28
  • 52
Frog
  • 1
  • So is there something wrong or not? If you debug it and there is nothing wrong it means there is nothing wrong. What does it mean "crashes"? What is the error message? – Maciej Jureczko Sep 01 '17 at 14:38
  • You have a never ending while loop. If you had tried debugging you would've probably noticed that. Please, try to take a look at the logic for your while loop and if you need to, use the console to log stuff and help you debug. – Ulises André Fierro Sep 01 '17 at 14:47

1 Answers1

4

This statement will never be false:

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

CurrentChord can never be "B♭ Major" and "G Major" at the same time. It's always not one of those 2.

To fix the condition, replace || with &&:

"While the chord isn't "B♭ Major" and the chord isn't "G Major" => "do stuff".

Also, preferably use !== instead of !=;

So, fixed:

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

Or:

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

Logically, they'll return the same value. Which you use is just a personal preference.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147