【问题标题】:Tampermonkey - insert random char randomly in a wordTampermonkey - 在单词中随机插入随机字符
【发布时间】:2018-05-26 00:54:54
【问题描述】:

我已经尝试了所有方法,从替换或尝试插入“。”在字符。 无法破解它,我的替换,我会丢失一两个字母,而 charat 只会将点放在我单词的前面或结尾。

假设我有 word='word' 我试图将返回值随机放置在提供的单词中。任何帮助或提示都会很棒。

编辑:如果有人想要,我使用了以下答案并进行了一些更改以满足我的需要:

function randomInt(max) {
return Math.floor(Math.random()*max);
}
function Insert(a, b)
{
var int = randomInt(4);
if (int !== -1)
{
return a.substr(0, int) + b + a.substr(int);
}
return "";}

将使用 Insert("word", ".");返回随机插入“。”在“词”中。再次感谢 B.Aimene 的帮助

【问题讨论】:

    标签: random character word


    【解决方案1】:

    你必须使用这个函数,变量b是'.' at 是位置。

    function Insert(a, b, at)
    {
    var position = a.indexOf(at); 
    
    if (position !== -1)
    {
        return a.substr(0, position) + b + a.substr(position);    
    }  
    return "Not found";}
    

    【讨论】:

    • 谢谢!我稍微编辑了一下,得到了我需要的东西!将在主帖中发布更新,以防其他人想看到它。
    猜你喜欢
    • 1970-01-01
    • 2019-09-08
    • 2015-05-07
    • 2022-07-21
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2020-02-24
    相关资源
    最近更新 更多