[英]I am having trouble with switch statements specifically making them check my code over and over
My code is only running my switch statement once and because of that the page doesn't refresh. 我的代码只运行一次switch语句,因此该页面不会刷新。 This makes it so once the code is run you only have those three buttons to press while what I want it to do it to update the switch statement multiple times since my variable I am checking is updating.
这样一来,一旦运行代码,您就只需要按下这三个按钮,而我想要它执行的操作就可以多次更新switch语句,因为我正在检查的变量正在更新。
let level = 1
if (level === 1) {
alert(level + '. You are walking down a path and see a shadowy figure emerge from the woods, what do you do?')
switch (level) {
case 1:
var button = document.createElement("button");
button.innerHTML = "Threaten";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener("click", function() {
alert("You threaten the man but are put to the ground and stabbed, you wake up hours later in a shack on the ocean with a women standing over you what do you do?");
level += 1
});
var button = document.createElement("button");
button.innerHTML = "Flee";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener("click", function() {
alert("You run like the wind to escape the man, you reach your house to tell your wife, she calls you crazy and leaves with your kids, what is your move?");
level += 2
});
var button = document.createElement("button");
button.innerHTML = "Attack";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener("click", function() {
alert("You take out a knife and plunge it through his chest, he falls to the ground, blood seeping from his vest, you run away, what is your move as you escape into the forest?");
level += 3
});
break;
case 2:
var button = document.createElement("button");
button.innerHTML = "Pay";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener("click", function() {
alert("did something");
});
var button = document.createElement("button");
button.innerHTML = "Threaten";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener("click", function() {
alert("did something");
});
break;
default:
alert('fail');
} }; };
I set up the switch properly but it only running case 1 and is not looking back at case two even though the variable is updating. 我正确设置了开关,但它只运行案例1,即使变量正在更新,它也没有回首案例2。 Do I have to put to switch statement in a for loop?
我是否必须在for循环中放入switch语句?
You might try something like this to shorten and simplify your code: 您可以尝试使用以下方法来缩短和简化代码:
var levels = [ [ 'You are walking down a path and see a shadowy figure emerge from the woods, what do you do?', {'Threaten': 1, 'Flee': 2, 'Attack': 3} ], [ 'You threaten the man but are put to the ground and stabbed, you wake up hours later in a shack on the ocean with a women standing over you what do you do?', {'Die': 4} ], [ 'You run like the wind to escape the man, you reach your house to tell your wife, she calls you crazy and leaves with your kids, what is your move?', {'Die': 4} ], [ 'You take out a knife and plunge it through his chest, he falls to the ground, blood seeping from his vest, you run away, what is your move as you escape into the forest?', {'Die': 4} ], [ 'You cannot die!', {'Start Over': 0} ], ], q = document.getElementById('q'), b = document.getElementById('b'), level = 0; play(level); function play(n = 0) { q.innerHTML = levels[n][0]; b.innerHTML = ''; for (var i in levels[n][1]) { var button = document.createElement('button'); button.name = levels[n][1][i]; button.innerHTML = i; button.addEventListener('click', function(){play(this.name)}); b.appendChild(button); } }
<h3 id="q"></h3> <div id="b"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.