我想从我的 <textarea> 输入中随机化单词中的字母

I want to randomize letters in words, from my <textarea> input

我想知道如何稍微更改我的文本区域输入中的单词。我不希望句子中的所有单词都被打乱,而只是(某些)单词中的(某些)字母。词序必须相同。

我正在考虑使用 var string_array = string.split("");但我找不到很多关于此的文档,也找不到任何其他对我有好处的选项。

有人对此有何建议?

<!--Made by MysteriousDuck#5764-->

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="textchanger.js"></script>
    <title>Text changer</title>
</head>

<body>

    <div class="container">

        <h1> Text Changer </h1>
        <h2> CAPS text changer</h2>

        <textarea type="text" autofocus="true" placeholder="input text" id="inputText" value="Input Value"
            spellcheck="false"></textarea>
        <button class="button button1" onclick="myConvertFunction()">Convert</button>
        <textarea type="text" placeholder="CoNvErTeD tExT" id="converted" value="Clear" readonly="true"
            spellcheck="false"></textarea>
        <button class="button button1" onclick="myCopyFunction(); eraseText();">Copy</button>

    </div>
</body>

</html>
/* Made by MysteriousDuck#5764 */

function myConvertFunction() {
    var x = document.getElementById("inputText").value;
    var foo = x.split("");
    var string = "";
    for (i = 0; i < foo.length; i++) {
        if (i % 2 == 0) {
            string += foo[i].toUpperCase();
        } else {
            string += foo[i];
        }
    }

    document.getElementById("converted").value = string;
}

function myCopyFunction() {
    var copyText = document.getElementById("converted");
    copyText.select();
    document.execCommand("copy");
    alert("Copied the text: " + copyText.value);
    eraseText();
}

function eraseText() {
    document.getElementById("converted").value = "";
    document.getElementById("inputText").value = "";
    document.getElementById("inputText").focus();
}

function randomizeLetters() {
    var x = document.getElementById("inputText").value;
}

您似乎希望将替代字符大写。如果是这种情况,那么您可以拆分 textarea 的值,然后在数组上使用 mapmap 将 return 一个新数组。在地图回调中检查角色的 index。如果是偶数,则转换为大写。由于 map return 是一个新数组,您可以使用 join 从数组

创建一个新字符串

function myConvertFunction() {
  var x = document.getElementById("inputText").value;
  var foo = x.split("");
  var string = foo.map(function(item, index) {
    if (index % 2 === 0) {
      return item.toUpperCase();
    } else {
      return item;
    }
  })

  document.getElementById("converted").value = string.join('');
}

function myCopyFunction() {
  var copyText = document.getElementById("converted");
  copyText.select();
  document.execCommand("copy");
  alert("Copied the text: " + copyText.value);
  eraseText();
}

function eraseText() {
  document.getElementById("converted").value = "";
  document.getElementById("inputText").value = "";
  document.getElementById("inputText").focus();
}

function randomizeLetters() {
  var x = document.getElementById("inputText").value;
}
<div class="container">

  <h1> Text Changer </h1>
  <h2> CAPS text changer</h2>

  <textarea type="text" autofocus="true" placeholder="input text" id="inputText" value="Input Value" spellcheck="false"></textarea>
  <button class="button button1" onclick="myConvertFunction()">Convert</button>
  <textarea type="text" placeholder="CoNvErTeD tExT" id="converted" value="Clear" readonly="true" spellcheck="false"></textarea>
  <button class="button button1" onclick="myCopyFunction(); eraseText();">Copy</button>

</div>

您可以使用

function scramble(text) {
    let words = text.split(' ');

    words = words.map(word => {
        if (word.length > 2) {
            return word.split('').sort(() => 0.5-Math.random()).join('');
        }

        return word;
    });

    console.log(words);

    return words.join(' ');
}

下面的函数接受一个字符串作为参数。随机打乱该字符串中的任何单词。还随机将单词中的任何字母大写

function randomizeString(sentence) {

    const words = sentence.split(' ');
    return words.map(word => {
        const flag = getRandomInt(2);
        return flag ? scrambleWord(word) : word;
    }).join(' ');

    function getRandomInt(max) {
        return Math.floor(Math.random() * Math.floor(max));
    }

    function scrambleWord (word) {
        var scramble = '';
        word = word.split('');
        while (word.length > 0) {
            scramble +=  word.splice(word.length * Math.random() << 0, 1);
        }
        return randomCapitalizeWord(scramble);
    }

    function randomCapitalizeWord(word) {
        return word.split('').map(letter => {
            const flag = getRandomInt(2);
            return flag ? letter.toUpperCase() : letter;
        }).join('');
    }
}