【发布时间】:2018-10-31 18:38:15
【问题描述】:
如何检查一个元素是否被使用,如果是,如何在 Switch Case 语句中使用另一个元素。
//Input
function myFunction() {
var myArray = ["60","50", "20", "30", "15", "10"];
var randomJumpingJacks = myArray[Math.floor(Math.random()*myArray.length)];
var randomCrunches = myArray[Math.floor(Math.random()*myArray.length)];
var workOut = document.getElementById("myInput").value;
var text = "";
for(const char of workOut.toUpperCase()){
switch(char) {
case "A":
case "I":
case "N":
case "X":
text += randomJumpingJacks;
text += " Jumping Jacks";
break;
case "B":
case "J":
case "Q":
case "Y":
text += randomCrunches;
text += " Crunches";
break;
case " ":
/*text += " break ";*/
break;
default:
text += "I have never heard of that fruit...";
}
text +=" "
text +="<br>"
}
document.getElementById("excercise").innerHTML = text;
}
<!DOCTYPE html>
<html>
<body>
<p>Input some characters and click the button.</p>
<p>Your excericse routine will display based on your input.</p>
<input id="myInput" type="text">
<button onclick="myFunction()">Try it</button>
<p>
<span id="reps"></span>
<span id="excercise"></span>
</p>
</body>
</html>
示例: 我输入:ABBY 我期待它输出:
10 个跳跃式千斤顶
15 次仰卧起坐
10 次仰卧起坐
20 次仰卧起坐
我不确定我是否使用了正确的术语,但我会将案例 A、I、N、X 称为 Jumping Block,将案例 B、J、Q、Y 称为 Crunches Block。
如果调用的字符来自同一块,则倾向于显示相同的随机数。
在 Crunches 块的第二个字符 (B) 上,它自然会输出 15,但由于它被用于第一个字符(也是 B),我希望它使用其他元素,
第三个字符 (Y) 也在 Crunches 块上,我希望它使用与前两个元素不同的不同元素,依此类推,并在 4 次后重复。
我很想知道你的想法。
【问题讨论】:
-
所以你将不得不改变你的逻辑来跟踪每个练习使用的数字。有很多方法可以做到。
标签: javascript arrays random switch-statement