Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 搜索句点并随机决定在50%的时间段后添加一个单词_Javascript_Html - Fatal编程技术网

Javascript 搜索句点并随机决定在50%的时间段后添加一个单词

Javascript 搜索句点并随机决定在50%的时间段后添加一个单词,javascript,html,Javascript,Html,我很难找到如何搜索文本区域并找到句点,在找到句点后,我需要随机决定是否在句点后添加“arrr”,这应该是50/50的机会在其中打印单词。 请帮帮我!!!到目前为止,我的情况是这样的,有点乱 document.getElementById(“translate”).onclick=函数已更改() { var outpara=document.getElementById(“out”) var段落=document.getElementById(“输入”).value; 段落=段落.toLowerC

我很难找到如何搜索文本区域并找到句点,在找到句点后,我需要随机决定是否在句点后添加“arrr”,这应该是50/50的机会在其中打印单词。 请帮帮我!!!到目前为止,我的情况是这样的,有点乱 document.getElementById(“translate”).onclick=函数已更改() {

var outpara=document.getElementById(“out”)
var段落=document.getElementById(“输入”).value;
段落=段落.toLowerCase();
而(段落索引(“你好”)!=-1)
{ 
段落=段落。替换(“你好”、“你好”);
}
while(段落索引(“对不起”)!=-1)
{
段落=段落。替换(“对不起”,“arrr”);
}
而(段落索引(“sir”)!=-1)
{
段落=段落。替换(“先生”、“朋友”);
}
而(段落索引(“女士”)!=-1)
{
段落=段落。替换(“女士”、“骄傲的美人”);
}
while(段落索引(“官员”)!=-1)
{
段落=段落。替换(“官员”、“肮脏的布拉加特”);
}
while(段落索引(“where is”)!==-1)
{
段落=段落。替换(“where is”,“whar be”);
}
while(段落索引(“你能帮我找到吗”)!=-1)
{
段落=段落。替换(“你能帮我找到吗”,“认识你”);
}
而(段落索引(“是”)!=-1)
{
段落=段落。替换(“是那”、“是那”);
}
而(段落索引(“the”)!=-1)
{
段落=段落。替换(“th”);
}
while(段落索引(“我的”)!=-1)
{
段落=段落。替换(“我的”、“我”);
}
while(段落索引(“您的”)!=-1)
{
段落=段落。替换(“您的”、“您的”);
}   
而(段落索引(“洗手间”)!=-1)
{
段落=段落。替换(“洗手间”、“头部”);
}   
而(段落索引(“餐厅”)!=-1)
{
段落=段落。替换(“餐厅”、“厨房”);
}
而(段落索引(“酒店”)!=-1)
{
段落=段落。替换(“酒店”、“跳蚤袋酒店”);
}
var x=分段(“”);
var repl=[Math.floor(Math.random()*2)]
如果(repl==0 | | repl==1)
第.款取代(“.”、“.arrr”)
其他的
第.款取代(“.,”)
var readySentance=[];
而(x.indexOf(“.”!=-1)
{
对于(变量i=0;i<段落长度;++i)
{
x、 推送(“Arrrr”);
}
}
我的HTML是这样的

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Pirate Translator</title>
<!--<link rel = "stylesheet" href="css/normalize.css" />
<link rel = "stylesheet" href="css/styles.css" />-->
</head>
<body>
<h1> Land Lovin' Lady's Pirate Translator</h1>
<p> Simply click on the buttons to translate words and/or phrases from English to  pirate talk</p><hr />
<form name = "pirateForm">

<div>   
<textarea id="input"></textarea>
<textarea id="out" readonly></textarea>
<input onclick= "changed();" type="button" id ="translate" value="translate"/>
<input onclick="clearArea2()" type="button" id="clear2" value= "clear"/>
<script src="pirate4.js"></script><br>

</form>
</div>
</body>
</html>

盗版翻译人员
《大地之恋》女海盗翻译人员
只需点击按钮,即可将英语单词和/或短语翻译成海盗语



使用正则表达式(以便您可以在替换中指定
g
全局标志)和回调:

paragraph = paragraph.replace(/\./g, function(){ return Math.random() < 0.5 ? ". arrrr" : "." });

这与你的问题无关,我不知道你是否已经准备好了,但是当代码不断重复时,让计算机工作的机会通常会被错过。实用程序员书中的原则是“不要重复你自己”(DRY)。
paragraph = paragraph.replace(/\./g, function(){ return Math.random() < 0.5 ? ". arrrr" : "." });
paragraph = paragraph.replace(/hello/g, "Ahoy");