简体   繁体   English

我已经尝试过,但是我的程序似乎不起作用

[英]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.

相关问题 我似乎无法让我的悬停效果在按钮上工作。 尝试了很多技巧 - i can't seem to get my hover effects to work on button. have tried many tips 我的 headerStyle 不起作用。 我试图分配背景颜色,但在我运行它时它没有出现 - My headerStyle doesn't work. I have tried to assign a background color though it doesn't appear when I run it 我需要一个带有可滚动主体的固定表头。 我已经尝试了一切,但似乎不起作用 - I need a fixed table header with a scrollable body. I have tried everything but it won't seem to work 我试图用 fetch 更新我的 ejs 页面,但它不起作用 - i tried to upate my ejs page with fetch but it doesn't work 我的块报价没有运行。 我也链接了 bootstrap css 和 js 文件,但它似乎不起作用 - My blockquote isn't running. I have link the bootstrap css and js files too but it doesn't seem to work 当我添加“”时,我尝试与我的 javascript 链接的按钮 onlick 似乎不起作用或在我的 vs 代码上突出显示橙色 - button onlick that i tried linking with my javascript doesn't seem to be working or highlighting orange on my vs code when i add the “” MathJax,我的代码似乎无法完全正常运行,有人可以看看吗? - MathJax, my code doesn't seem to work completely fine, can anybody have a look? SweetAlert2 似乎对我不起作用。 我做错了什么? - SweetAlert2 doesn't seem to work for me . What have i done wrong? 我试图拖放一个元素。 为什么我的代码不能正常工作? - I have tried to drag and drop an element. Why won't my code work properly? 我的切换器不工作。 我试过将 js cdn 放在顶部和底部,但没有用 - My toggler is not working. I have tried placing js cdn in top and bottom both but didn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM