【问题标题】:Check if one element in array is being used, if so, use another element检查是否正在使用数组中的一个元素,如果是,则使用另一个元素
【发布时间】: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


【解决方案1】:

您需要在 for 循环内移动随机逻辑。要为字符串中的每个字母获取一个新的随机数,您需要在每次循环通过时重新分配该值。请参阅下面的代码。

function myFunction() {
  
var myArray = ["60","50", "20", "30", "15", "10"];

   
    var workOut = document.getElementById("myInput").value;
  
  
var text = "";
for(const char of workOut.toUpperCase()){
  var randomJumpingJacks = myArray[Math.floor(Math.random()*myArray.length)];
  var randomCrunches = myArray[Math.floor(Math.random()*myArray.length)];

    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>

【讨论】:

  • 谢谢,它运行得很好,但说实话,我不明白其中的逻辑,你能给我解释一下吗?
  • 当然@Ebizl,当你在循环之前分配变量时,它在循环中总是有相同的变量。但是在每个循环开始时获取随机数会随着循环的每次循环而改变它的值。
  • 让我感到困惑的是,将它放在 for 块中会导致这种差异。非常感谢
  • @Ebizl 认为它是这样的。 for 块中的代码每次循环时都会运行。因此,如果循环运行 5 次,for 块中的所有代码将从开始到结束运行 5 次。 for 块外的代码只运行一次。
猜你喜欢
  • 2016-06-02
  • 2020-12-03
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
  • 2021-01-30
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多