Jak sprawdzić, czy jeden element jest używany, a jeśli tak, to użyć innego elementu w instrukcjach 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>
Przykład: wpisałem: ABBY Spodziewam się, że wypisze:
10 pajacyków
15 brzuszków
10 brzuszków
20 brzuszków
Nie jestem pewien, czy używam poprawnej terminologii, ale przypadki A,I,N,X będę określać jako blok skokowy, a przypadki B,J,Q,Y jako blok brzuszków.
Jeśli wywoływane znaki pochodzą z tego samego bloku, istnieje tendencja do wyświetlania tej samej liczby losowej.
Na drugim znaku (B) bloku Crunches naturalnie wypisuje 15, ale ponieważ jest używany dla pierwszego znaku (również B), chciałbym użyć innego elementu,
A trzeci znak (Y) jest również w bloku Crunches, chciałbym użyć innego elementu, który różni się od dwóch pierwszych elementów i tak dalej, i powtórzyć po 4 razy.
Chciałbym wiedzieć, co myślisz.
1 odpowiedź
Musisz przenieść losową logikę wewnątrz pętli for. Aby uzyskać nową liczbę losową dla każdej litery w ciągu, musisz ponownie przypisać wartość za każdym razem, gdy przechodzi pętla. Zobacz kod poniżej.
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>