我很难弄清楚如何通过 textarea 搜索并找到句点,在找到我需要随机决定是否应该在句点之后添加“arrr”的句点之后,这应该是 50/50 的机会里面印着的字。请帮帮我!!!这是我到目前为止所做的事情,有点混乱 document.getElementById("translate").onclick = function changed() {
var outpara = document.getElementById("out")
var paragraph = document.getElementById("input").value;
paragraph = paragraph.toLowerCase();
while(paragraph.indexOf("hello")!== -1)
{
paragraph = paragraph.replace("hello", "Ahoy");
}
while(paragraph.indexOf("excuse me")!== -1)
{
paragraph = paragraph.replace("excuse me", "arrr");
}
while(paragraph.indexOf("sir")!== -1)
{
paragraph = paragraph.replace("sir", "matey");
}
while(paragraph.indexOf("madam")!== -1)
{
paragraph = paragraph.replace("madam", "proud beauty");
}
while(paragraph.indexOf("officer")!== -1)
{
paragraph = paragraph.replace("officer", "foul blaggart");
}
while(paragraph.indexOf("where is")!== -1)
{
paragraph = paragraph.replace("where is", "whar be");
}
while(paragraph.indexOf("can you help me find")!== -1)
{
paragraph = paragraph.replace("can you help me find", "know ye");
}
while(paragraph.indexOf("is that")!== -1)
{
paragraph = paragraph.replace("is that", "be that");
}
while(paragraph.indexOf("the")!== -1)
{
paragraph = paragraph.replace("the", "th'");
}
while(paragraph.indexOf("my")!== -1)
{
paragraph = paragraph.replace("my", "me");
}
while(paragraph.indexOf("your")!== -1)
{
paragraph = paragraph.replace("your", "yer");
}
while(paragraph.indexOf("restroom")!== -1)
{
paragraph = paragraph.replace("restroom", "head");
}
while(paragraph.indexOf("restaurant")!== -1)
{
paragraph = paragraph.replace("restaurant", "galley");
}
while(paragraph.indexOf("hotel")!== -1)
{
paragraph = paragraph.replace("hotel", "fleabag inn");
}
var x = paragraph.split(" ");
var repl= [Math.floor (Math.random()* 2)]
if(repl === 0 || repl === 1)
paragraph.replace(".", ". arrrr")
else
paragraph.replace(".", ".")
var readySentance=[];
while(x.indexOf(".")!==-1)
{
for(var i = 0; i < paragraph.length; ++i)
{
x.push(". 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>