猪拉丁语翻译 - JavaScript [英] Pig Latin Translator - JavaScript
问题描述
因此,对于我的cit类,我必须编写一个猪拉丁语转换程序,我真的很困惑如何将数组和字符串一起使用。
转换的规则很简单,你只需将单词的第一个字母移到后面,然后添加一个。 ex:hell in English will ellhay in pig Latin
到目前为止我有这个:
So for my cit class I have to write a pig Latin converter program and I'm really confused on how to use arrays and strings together. The rules for the conversion are simple, you just move the first letter of the word to the back and then add ay. ex: hell in English would be ellhay in pig Latin I have this so far:
<form name="form">
<p>English word/sentence:</p> <input type="text" id="english" required="required" size="80" /> <br />
<input type="button" value="Translate!" onClick="translation()" />
<p>Pig Latin translation:</p> <textarea name="piglat" rows="10" cols="60"></textarea>
</form>
<script type="text/javascript">
<!--
fucntion translation() {
var delimiter = " ";
input = document.form.english.value;
tokens = input.split(delimiter);
output = [];
len = tokens.length;
i;
for (i = 1; i<len; i++){
output.push(input[i]);
}
output.push(tokens[0]);
output = output.join(delimiter);
}
//-->
</script>
我真的很感激我能得到任何帮助!
I'd really appreciate any help I can get!
推荐答案
我认为你真正需要关注的两件事是 substring()
方法和字符串连接(一般来说,两个字符串加在一起)。由于您对 split()
的调用返回的数组中的所有对象都是字符串,因此简单的字符串连接工作正常。例如,使用这两种方法,您可以将字符串的第一个字母移动到最后,如下所示:
I think the two things you really need to be looking at are the substring()
method and string concatentation (adding two strings together) in general. Being that all of the objects in the array returned from your call to split()
are strings, simple string concatentation works fine. For example, using these two methods, you could move the first letter of a string to the end with something like this:
var myString = "apple";
var newString = mystring.substring(1) + mystring.substring(0,1);
这篇关于猪拉丁语翻译 - JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!