js-wordmixletter/mix.html

35 lines
1.3 KiB
HTML
Raw Permalink Normal View History

2024-11-20 15:55:44 +02:00
<!DOCTYPE html>
<html lang="fi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sanakirjainten sekoittaminen</title>
<script>
function shuffleWord(word) {
if (word.length <= 3) {
return word; // Jätetään pienet sanat ennalleen
}
let middle = word.slice(1, -1).split('');
for (let i = middle.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[middle[i], middle[j]] = [middle[j], middle[i]];
}
return word[0] + middle.join('') + word[word.length - 1];
}
function shuffleText() {
let inputText = document.getElementById("inputText").value;
let words = inputText.split(' ');
let shuffledWords = words.map(word => shuffleWord(word));
document.getElementById("outputText").innerText = shuffledWords.join(' ');
}
</script>
</head>
<body>
<h1>Sanojen keskimmäisten kirjainten sekoittaminen</h1>
<textarea id="inputText" rows="4" cols="50" placeholder="Kirjoita teksti tähän..."></textarea><br><br>
<button onclick="shuffleText()">Sekoita teksti</button><br><br>
<p><strong>Sekoitetut sanat:</strong></p>
<p id="outputText"></p>
</body>
</html>