带有多个字符串输入的开关盒

时间:2018-10-31 13:41:57

标签: javascript

我正在努力让用户输入基本上多个字符串(字母),并根据输入框中放入的每种情况的内容输出结果。 例: 我在框中输入:HELLO: 输出: 下蹲 墙坐 墙坐 墙坐 伯比犬

有可能吗? 如果没有,有什么替代方法。

//Input
function myFunction() {
    var text;
  var output;
 
    var fruits = document.getElementById("myInput").value;

    switch(fruits.toUpperCase()) {
        case "A":
              output="Sample Text";
        case "I":
        case "N":
        case "X":
            text = "Jumping Jacks";
        break;
        case "B":
        case "J":
        case "Q":
        case "Y":
        text = "Crunches";
        break;
        case "C":
        case "H":
        case "T":
        text = "Squats";
        break;
        case "D":
        case "K":
        case "R":
        case "Z":
            text = "Push ups";
        break;
        case "E":
        case "L":
        case "V":
            text = "Wall Sits";
        break;
        case "F":
        case "M":
        case "O":
        case "S":
            text = "Burpees";
        break;
        case "G":
        case "P":
        case "U":
        case "W":
            text = "Arm Circles";
        break;
        default:
        text = "I have never heard of that fruit...";
    }
    document.getElementById("excercise").innerHTML = text;
  
  
  ////////Random Generator for Reps/////////
var myArray = ["60","50", "20", "30", "15", "10"];

var randomItem = myArray[Math.floor(Math.random()*myArray.length)];

document.getElementById("reps").innerHTML = randomItem;
  ////////////////////////////////////
}
<!DOCTYPE html>
<html>
<body>

<p>Write a letter in the input field and click the button.</p>
<p>The switch statement will execute a block of code 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>

您可以在这里https://codepen.io/ebizl/pen/PyrQmZ

找到我的代码

3 个答案:

答案 0 :(得分:0)

您只需打开输入单词的每个字符:

 var text = "";
 for(const char of fruit.toUpperCase()) {
  switch(char) {
    case "A":
       text += "Sample Text";
       break;

    case "I":
    case "N":
    case "X":
        text += "Jumping Jacks";
        break;

    case "B":
    case "J":
    case "Q":
    case "Y":
      text += "Crunches";
      break;

    case "C":
    case "H":
    case "T":
      text += "Squats";
      break;

    case "D":
    case "K":
    case "R":
    case "Z":
       text += "Push ups";      
       break;

    case "E":
    case "L":
    case "V":
        text += "Wall Sits";
        break;

    case "F":
    case "M":
    case "O":
    case "S":
        text += "Burpees";
        break;

    case "G":
    case "P":
    case "U":
    case "W":
        text += "Arm Circles";
       break;

    default:
       text += "I have never heard of that fruit...";
       break;
  }
  text += "<br />";
 }

答案 1 :(得分:0)

您想检查整个输入,但您的代码不需要

这是您应该执行的步骤:

  • 将输入值拆分为chars
  • 将结果数组从split中循环出来,以检查每个字符以及您要使用的函数
  • 连接结果并显示

答案 2 :(得分:0)

尝试以下方式:

var fruits = document.getElementById("myInput").value;
for(var i = 0; i < fruits.length; i++)
   console.log(getValue(fruits[i]));

function getValue(c){
var output = "", text = "";
    switch(c.toUpperCase()) {
        case "A":
              output="Sample Text";
        case "I":
        case "N":
        case "X":
            text = "Jumping Jacks";
        break;
        case "B":
        case "J":
        case "Q":
        case "Y":
        text = "Crunches";
        break;
        case "C":
        case "H":
        case "T":
        text = "Squats";
        break;
        case "D":
        case "K":
        case "R":
        case "Z":
            text = "Push ups";
        break;
        case "E":
        case "L":
        case "V":
            text = "Wall Sits";
        break;
        case "F":
        case "M":
        case "O":
        case "S":
            text = "Burpees";
        break;
        case "G":
        case "P":
        case "U":
        case "W":
            text = "Arm Circles";
        break;
        default:
        text = "I have never heard of that fruit...";
    }
    return {output, text };
}