English 中文(简体)
Pig Latin Translator - Javagust
原标题:Pig Latin Translator - JavaScript
  • 时间:2012-04-24 22:24:42
  •  标签:
  • javascript

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>

我真的赞赏我能得到的任何帮助!

最佳回答

我认为,你确实需要研究的两点是:<编码>(<>>substring(>)方法,以及一般的拼凑(合两条)。 由于从你打电话到<代码>split(>)的阵列中的所有物体都在铺设,简单明了的拼凑合物可被罚款。 例如,使用这两种方法,你可以把第一封扼杀信最后换成这样的内容:

var myString = "apple";

var newString = mystring.substring(1) + mystring.substring(0,1);
问题回答
function translate(str) {
     str=str.toLowerCase();
     var n =str.search(/[aeiuo]/);
     switch (n){
       case 0: str = str+"way"; break;
       case -1: str = str+"ay"; break;
       default :
         //str= str.substr(n)+str.substr(0,n)+"ay";
         str=str.replace(/([^aeiou]*)([aeiou])(w+)/, "$2$3$1ay");
       break;
    }
    return str;

}


 translate("paragraphs")

该法典是基本法,但行之有效。 首先,注意从wel开始的话。 否则,从一个或多个共犯开始的言词决定了共犯人数,将其移至尾。

function translate(str) {
    str=str.toLowerCase();

    // for words that start with a vowel:
    if (["a", "e", "i", "o", "u"].indexOf(str[0]) > -1) {
        return str=str+"way";
    }

    // for words that start with one or more consonants
   else {
   //check for multiple consonants
       for (var i = 0; i<str.length; i++){
           if (["a", "e", "i", "o", "u"].indexOf(str[i]) > -1){
               var firstcons = str.slice(0, i);
               var middle = str.slice(i, str.length);
               str = middle+firstcons+"ay";
               break;}
            }
    return str;}
}

translate("school");

如果你重新努力处理阵列,这可能是一个很复杂的问题,但是它简明扼要。

var toPigLatin = function(str) {
    return str.replace(/(^w)(.+)/,  $2$1ay );
};

Demo:

a. 稍微改进的版本,供整个判决使用:

var toPigLatin = function(str){
    return str.replace(/(w)(w+)/g,  $2$1ay );
};

在此,我的解决办法

function translatePigLatin(str) {
  var newStr = str;
  // if string starts with vowel make  way  adjustment
  if (newStr.slice(0,1).match(/[aeiouAEIOU]/)) {
    newStr = newStr + "way";
  }
  // else, iterate through first consonents to find end of cluster
  // move consonant cluster to end, and add  ay  adjustment
  else {
    var moveLetters = "";
    while (newStr.slice(0,1).match(/[^aeiouAEIOU]/)) {
      moveLetters += newStr.slice(0,1);
      newStr = newStr.slice(1, newStr.length);
    }
    newStr = newStr + moveLetters + "ay";
  }
  return newStr;
}

采取另一种做法,将单独职能作为真正的或虚假的交换。

function translatePigLatin(str) {

  // returns true only if the first letter in str is a vowel
  function isVowelFirstLetter() {
    var vowels = [ a ,  e ,  i ,  o ,  u ,  y ];
    for (i = 0; i < vowels.length; i++) {
      if (vowels[i] === str[0]) {
        return true;
      }
    }
    return false;
  }

  // if str begins with vowel case
  if (isVowelFirstLetter()) {
    str +=  way ;
  }
  else {
    // consonants to move to the end of string
    var consonants =   ;

    while (isVowelFirstLetter() === false) {
      consonants += str.slice(0,1);
      // remove consonant from str beginning
      str = str.slice(1);
    }

    str += consonants +  ay ;
  }

  return str;
}

translatePigLatin("jstest");

这是我的解决办法守则:

function translatePigLatin(str) {
var vowel;
var consonant;
var n =str.charAt(0);
vowel=n.match(/[aeiou]/g);
if(vowel===null)
{
consonant= str.slice(1)+str.charAt(0)+”ay”;
}
else
{
consonant= str.slice(0)+”way”;
}
var regex = /[aeiou]/gi;
var vowelIndice = str.indexOf(str.match(regex)[0]);
if (vowelIndice>=2)
{
consonant = str.substr(vowelIndice) + str.substr(0, vowelIndice) + ‘ay’;
}

return consonant;
}

translatePigLatin(“gloove”);

另一种方式。

String.prototype.toPigLatin = function()
{
    var str = "";
    this.toString().split(   ).forEach(function(word)
    {
        str += (toPigLatin(word) +    ).toString();
    });
    return str.slice(0, -1);
};

function toPigLatin(word)
{
    //Does the word already start with a vowel?
    if(word.charAt(0).match(/a*e*i*o*u*A*E*I*O*U*/g)[0])
    {
        return word;
    }

    //Match Anything before the firt vowel.
    var front = word.match(/^(?:[^a?e?i?o?u?A?E?I?O?U?])+/g);
    return (word.replace(front, "") + front + (word.match(/[a-z]+/g) ?  a  :   )).toString();
}

拉丁美洲另一种方式:

function translatePigLatin(str) {
  let vowels = /[aeiou]/g;
  var n = str.search(vowels); //will find the index of vowels
  switch (n){
    case 0:
      str = str+"way";
      break;
    case -1:
      str = str+"ay";
      break;
    default:
      str = str.slice(n)+str.slice(0,n)+"ay";
      break;
  }
  return str;
}

console.log(translatePigLatin("rhythm"));

您的朋友是:斯特鲁特职能(>.split)和阵列职能(.join.slice.concat

<>预警:Below is a full Solutions You can refer to when You have contributed orpent som>.

function letters(word) {
    return word.split(  )
}

function pigLatinizeWord(word) {
    var chars = letters(word);
    return chars.slice(1).join(  ) + chars[0] +  ay ;
}

function pigLatinizeSentence(sentence) {
    return sentence.replace(/w+/g, pigLatinizeWord)
}

Demo:

> pigLatinizeSentence( This, is a test! )
"hisTay, siay aay esttay!"




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.