每当我运行此代码时,浏览器都会崩溃。我犯了一个愚蠢的错误,或者我的代码是否存在严重错误? 我尽可能地尝试调试我的代码,但是当通过调试器时,我的代码显然没有任何问题。怎么了?非常感激您的帮忙。
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>
答案 0 :(得分:4)
此声明永远不会成为false
:
while(CurrentChord!="B♭ Major" || CurrentChord!="G Major"){
CurrentChord
永远不能同时"B♭ Major"
和 "G Major"
。它总是不其中之一。
要解决此问题,请将||
替换为&&
:
“虽然和弦不是”B♭Major“且,但和弦不是”G Major“ =&gt; ”做东西“< / em>的
此外,preferably使用!==
代替!=
;
所以,修复:
while(CurrentChord !== "B♭ Major" && CurrentChord !== "G Major"){
或者:
while(!(CurrentChord === "B♭ Major" || CurrentChord === "G Major")){
逻辑上,它们将返回相同的值。您使用的只是个人偏好。