[英]I have tried but my program doesn't seem to work
I have created a system to translate english into pig latin, but it doesn't work. 我创建了一个将英语翻译成猪拉丁语的系统,但是它不起作用。 The problem is that the variable
fl
isn't showing. 问题是变量
fl
没有显示。 Does anyone know how to fix this? 有谁知道如何解决这一问题?
function piglatinify() { var word = document.getElementById("textbox").value; var fl = word.substr(1, 1); var wordwithoutfl = word.substr(2, 999); var piglatinword = wordwithoutfl.concat(fl); var piglatinword = piglatinword.concat("ay"); document.write(piglatinword); }
<textarea id="textbox"></textarea><br> <button onclick="piglatinify()">See it in pig latin...</button>
The length parameter of the substr() method is optional. substr()方法的length参数是可选的。 Change this:
更改此:
var wordwithoutfl = word.substr(2, 999);
to this: 对此:
var wordwithoutfl = word.substr(2);
Also, condense these lines: 另外,压缩这些行:
var piglatinword = wordwithoutfl.concat(fl);
var piglatinword = piglatinword.concat("ay");
to this: 对此:
var piglatinword = wordwithoutfl + fl + "ay";
For two reasons: 1) This makes it so that you are not declaring the variable piglatinword
twice, and 2) the "+" operator has better performance than the concat() method (see this page on the Mozilla Developer Network). 出于两个原因:1)这样做是为了
piglatinword
再次声明变量piglatinword
; 2)“ +”运算符的性能比concat()方法更好(请参见Mozilla开发人员网络上的此页 )。
A short way to do it would be: 一个简短的方法是:
function piglatinify() {
var word = document.getElementById("textbox").value;
var piglatinword = word.substr(1) + word.substr(0, 1) + "ay";
document.write(piglatinword);
}
Also, remember that Javascript starts counting at 0, so to get the first letter, you need to do substr(0, 1). 另外,请记住Javascript从0开始计数,因此要获得第一个字母,您需要执行substr(0,1)。 substr(1, 1) will get you the second letter.
substr(1,1)将给您第二个字母。
word.substr(2, 999) can be changed to word.substr(2), as the length parameter is optional, but that would get you the string, minus the first 2 letters (qwerty => erty). word.substr(2,999)可以更改为word.substr(2),因为length参数是可选的,但这会使您得到字符串,减去前2个字母(qwerty => erty)。 Therefore, you would need to make it word.substr(1).
因此,您需要将其设置为word.substr(1)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.