mercredi 31 octobre 2018

Check if one element in array is being used, if so, use another element

How do I check if one element is being used, and if so to use another element on the Switch Case statements.

//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>

Example: I typed: ABBY I'm expecting it to output:

10 Jumping Jacks

15 Crunches

10 Crunches

20 Crunches

I'm unsure if I'm using the correct terminology, but I'm going to refer cases A,I,N,X as Jumping Block, and cases B,J,Q,Y as Crunches Block.

If the chars called are from the same block, there's a tendency to display the same random number.

On the second Char (B) of the Crunches block, it naturally outputs 15, but since it is being used for the first char (also B), I'd like it to use some other element,

and the third char (Y) being on the Crunches block as well, I'd like it to use a different element that are different from the first two elements, and so forth, and repeat after 4 times.

I'd love to know what you think.




Aucun commentaire:

Enregistrer un commentaire