www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: [RESOLVED] Help required with a line of code

  1. #1
    Join Date
    Jun 2011
    Posts
    4

    resolved [RESOLVED] Help required with a line of code

    Hi Everybody,

    Could somebody please take a look at the lines of coding below and advise on how to complete it? It's a code to translate a piece of text, I kinda figured some of it out, but could use some help!

    The first part is to translate 'the cat sat on the mat' to 'thube cubat subat ubon thube mubat'.
    The second part is entering text and clicking a button to translate.

    Many thanks in advance!

    First part - translating cat on the mat:
    function isVowel(aCharacter)
    {
    return ((aCharacter == 'a') || (aCharacter == 'A')||
    (aCharacter == 'e') || (aCharacter == 'E')||
    (aCharacter == 'i') || (aCharacter == 'I')||
    (aCharacter == 'o') || (aCharacter == 'O')||
    (aCharacter == 'u') || (aCharacter == 'U')||
    (aCharacter == 'y') || (aCharacter == 'Y'));
    }


    /*
    * translates a word into Ubbi Dubbi
    */

    function ubbiDubbi(aString)
    {
    // variable to hold resultString
    var resultString = '';

    // variable to hold the current and previous characters
    var currentCharacter;
    var precedingCharacter;

    // in the case of the first character in the string there is no
    // previous character, so we assign an empty string '' to the variable at first
    precedingCharacter = '';


    for(currentCharacter = 0; currentCharacter < aString.length; currentCharacter++, precedingCharacter = currentCharacter-1)
    {
    if (isVowel(aString[currentCharacter]) && (!isVowel(aString[precedingCharacter])) );
    {
    resultString += "ub";
    }
    resultString += aString[currentCharacter];
    }
    return resultString;
    }

    // TEST
    var string1 = 'the cat sat on the mat';
    var result1 = ubbiDubbi(string1); // result should be 'thube cubat subat ubon thube mubat'
    document.write(string1)
    document.write('<BR>');
    document.write(result1)
    document.write('<BR>');
    if (result1 == 'thube cubat subat ubon thube mubat')
    {
    document.write('test passed');
    }
    document.write('<BR>');

    Second Part has the below HTML codes available, but requires the following structured english to be converted into code:

    access the value of the textbox original
    if the value equals the empty string
    issue a warning in an alert box
    else
    set the value of the translation textbox to be the original value translated into Ubbi Dubbi
    end if



    <FORM NAME = "translator">
    Please enter a phrase
    <BR>
    <INPUT TYPE = "text"
    NAME = "original"
    SIZE = "40"
    VALUE = ''>
    <BR>

    <INPUT TYPE = "button"
    VALUE = "Translate into Ubbi Dubbi"
    ONCLICK = "translateIntoUbbiDubbi();">
    <BR>
    Translation into Ubbi Dubbi is
    <BR>
    <INPUT TYPE = "text"
    NAME = "translation"
    SIZE = "40"
    VALUE = ''>

    </FORM>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,263
    What are the actual "rules" for the translation?

    Seems similar to Piglatin, but different.

  3. #3
    Join Date
    Jun 2011
    Posts
    4
    I am not to sure what you mean by that, but I'll try and explain what I expect from the coding a bit better.

    For the first bit I want a line of coding that translates and outputs pieces of text from variables into Ubbi Dubbi. The code is supposed to identify vowels, using loops, and inserting 'ub' in front of the vowels. Then 2 variables are set to test the code. First 'string1' - the cat is on the mat, second 'result1' - the cat is on the mat translated into Ubbi Dubbi (so; thube cubat subat ubon thube mubat)

    The second bit of coding is a bit like the first, but includes HTML and has a skeleton function. The skeleton function is to call on var result1 = ubbiDubbi. The FORM "translator" is supposed to output text entered into the input text box, a translate button then translates the inserted text into Ubbi Dubbi. When there is no text entered a window.alert is prompted. The code in written English would be:

    access the value of the textbox original
    if the value equals the empty string
    issue a warning in an alert box
    else
    set the value of the translation textbox to be the original value translated into Ubbi Dubbi
    end if

    I hope that makes a bit more sense?

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,263

    Question

    Quote Originally Posted by WindowLicker View Post
    I am not to sure what you mean by that, but I'll try and explain what I expect from the coding a bit better.
    ...
    I hope that makes a bit more sense?
    Not really ... It appears that 'ub' goes in front of the 1st vowel
    Is this the only rule as you have given words with only one vowel?

    By rules I mean: What happens to words with more than one vowel?
    Words like:
    receive ... should it be 'rubceive' or 'rubcubeubiveub'?
    meat ... should it be 'mubeat' or 'mubeubat'?

  5. #5
    Join Date
    Jun 2011
    Posts
    4
    I see wat you mean, the rule is to insert 'ub'before each vowel

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,263

    Lightbulb

    Try this:
    Code:
    <html>
    <head>
    <title>Ubbi Dubbi Translator</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?p=1161312#post1161312
    
    function toUbbiDubbi(wrd) {
      var str = '';
      var tmp = wrd.split('');
      for (var i=0; i<wrd.length; i++) {
        if ('aeiouyAEIOUY'.indexOf(wrd[i]) == -1) { str += wrd[i]; }
                                             else { str += 'ub'+wrd[i]; }
      }
      return str;
    }
    
    function translateIntoUbbiDubbi() {
      var rslt = '';
      var words = document.getElementById('original').value.split(' ');
      for (var i=0; i<words.length; i++) {
        rslt += toUbbiDubbi(words[i])+' ';
      }
      document.getElementById('translation').value = rslt;
    }
    
    // following modified from: http://gwydir.demon.co.uk/jo/javascript/numbers.htm#piglatin
    function toPiglatin() {
      var word = document.getElementById('original').value.split(" ");
      var str = "";
      for (i = 0; i < word.length; i++) {
        thisword = word [i];
        str = str + thisword.substring (1, thisword.length) + thisword.substring(0,1) + "ay "
      }
      document.getElementById('translation').value = str;
    } 
    
    </script>
    </head>
    <body>
    Please enter a phrase<BR>
    <INPUT TYPE="text" id="original" SIZE="40"
     VALUE='The cat sat on the mat'>
    
    <p><INPUT TYPE="button" VALUE="Translate into Ubbi Dubbi"
        ONCLICK="translateIntoUbbiDubbi();">
       <INPUT TYPE="button" VALUE="Translate into Piglatin"
        ONCLICK="toPiglatin();">
    
    <p>
    Translation into Ubbi Dubbi is
    <BR><INPUT TYPE="text" id="translation" SIZE="40"
     VALUE='' readonly>
    
    </body>
    </html>

  7. #7
    Join Date
    Jun 2011
    Posts
    4
    Cheers!

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,263

    Thumbs up

    Quote Originally Posted by WindowLicker View Post
    Cheers!
    You're most welcome.
    Happy to help once I can figure out what you want.
    Good Luck!

  9. #9
    Join Date
    Jun 2011
    Posts
    5
    Hi

    Thanks for the code.
    i'm trying to put it to work with a NAME instead of ID in the form. Can you please help?

    Also, how can I add a validation in the form to pop-up an alert if no string is inserted into the form?

    Thanks

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,263

    Exclamation

    Quote Originally Posted by John Smith View Post
    Hi

    Thanks for the code.
    i'm trying to put it to work with a NAME instead of ID in the form. Can you please help?

    Also, how can I add a validation in the form to pop-up an alert if no string is inserted into the form?

    Thanks
    I don't understand why you would need a <form>?

    You should:
    1. Show the code you are using
    and
    2. Start your own thread as this OP feels the problem is resolved.
    Often times other forum members will not feel like looking at resolved problems.

  11. #11
    Join Date
    Jun 2011
    Posts
    5
    Sorry we are working on the same topic.

    The idea is to use a form and so use a getElementByName and not getElementById.

    Also where would you place the window.alert?

    Thanks

  12. #12
    Join Date
    Jun 2011
    Posts
    5
    The FORM is:

    <FORM NAME = "translator">

    Please enter a phrase
    <BR>
    <INPUT TYPE = "text"
    ID = "original"
    SIZE = "40"
    VALUE = ''>
    <BR>

    <!-- the ONCLICK attribute is assigned the function translateIntoUbbiDubbi()-->

    <INPUT TYPE = "button"
    VALUE = "Translate into Ubbi Dubbi"
    ONCLICK = "translateIntoUbbiDubbi();">
    <BR>
    Translation into Ubbi Dubbi is
    <BR>
    <INPUT TYPE = "text"
    ID = "translation"
    SIZE = "40"
    VALUE = ''>

  13. #13
    Join Date
    Jun 2011
    Posts
    5
    Sorry the correct for is:

    <FORM NAME = "translator">

    Please enter a phrase
    <BR>
    <INPUT TYPE = "text"
    NAME = "original"
    SIZE = "40"
    VALUE = ''>
    <BR>

    <!-- the ONCLICK attribute is assigned the function translateIntoUbbiDubbi()-->

    <INPUT TYPE = "button"
    VALUE = "Translate into Ubbi Dubbi"
    ONCLICK = "translateIntoUbbiDubbi();">
    <BR>
    Translation into Ubbi Dubbi is
    <BR>
    <INPUT TYPE = "text"
    NAME = "translation"
    SIZE = "40"
    VALUE = ''>

    </FORM>

  14. #14
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,263

    Exclamation

    I still see no need for the <form> as you don't seem to be submitting the information to anywhere.

    But the following does exactly the same thing as the first posting.
    Code:
    <html>
    <head>
    <title>Ubbi Dubbi Translator</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?p=1161312#post1161312
    
    function toUbbiDubbi(wrd) {
      var str = '';
      var tmp = wrd.split('');
      for (var i=0; i<wrd.length; i++) {
        if ('aeiouyAEIOUY'.indexOf(wrd[i]) == -1) { str += wrd[i]; }
                                             else { str += 'ub'+wrd[i]; }
      }
      return str;
    }
    
    function translateIntoUbbiDubbi() {
      var rslt = '';
      var words = document.myForm.original.value; 
      if (words == '') { alert('Nothing to translate'); return; }
    
      var word = words.split(" ");
      for (var i=0; i<word.length; i++) {
        rslt += toUbbiDubbi(word[i])+' ';
      }
      document.myForm.translation.value = rslt;
    }
    
    // following modified from: http://gwydir.demon.co.uk/jo/javascript/numbers.htm#piglatin
    function toPiglatin() {
      var words = document.myForm.original.value;
      if (words == '') { alert('Nothing to translate'); return; }
    
      var word = words.split(" ");
      var str = "";
      for (i=0; i<word.length; i++) {
        thisword = word [i];
        str = str + thisword.substring (1, thisword.length) + thisword.substring(0,1) + "ay "
      }
      document.myForm.translation.value = str;
    } 
    
    </script>
    </head>
    <body>
    <form name="myForm" action="" method="post" onsubmt="return false">
    
    Please enter a phrase<BR>
    <INPUT TYPE="text" name="original" SIZE="40"  VALUE='The cat sat on the mat'>
    
    <p><INPUT TYPE="button" VALUE="Translate into Ubbi Dubbi"
        ONCLICK="translateIntoUbbiDubbi();">
       <INPUT TYPE="button" VALUE="Translate into Piglatin"
        ONCLICK="toPiglatin();">
    
    <p>
    Translation into Ubbi Dubbi is
    <BR><INPUT TYPE="text" name="translation" SIZE="40" VALUE='' readonly>
    
    </form>
    
    </body>
    </html>

  15. #15
    Join Date
    Jun 2011
    Posts
    5
    Thanks if I replace your function toUbbiDubbi(wrd) with the provided one

    function ubbiDubbi(aString)
    {
    // variable to hold resultString
    var resultString = '';

    // variable to hold the current and previous characters
    var currentCharacter;
    var precedingCharacter;

    // in the case of the first character in the string there is no
    // previous character, so we assign an empty string '' to the variable at first
    precedingCharacter = '';


    for(currentCharacter = 0; currentCharacter < aString.length; currentCharacter++, precedingCharacter = currentCharacter-1)
    {
    if (isVowel(aString[currentCharacter]) && (!isVowel(aString[precedingCharacter])) );
    {
    resultString += "ub";
    }
    resultString += aString[currentCharacter];
    }
    return resultString;
    }

    the code breaks... Any suggestion?

    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles