Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Function into IF. Don't work

    HI all!
    I have this code, but when I running, the program ignore the function vocals and it run without remove the vocals!!! Can you fund my error?
    Thanks All
    Code:
                function vocals ()
                {
                    var v;
                    v = "a" + "e" + "i" + "o" + "u";
                }
                
                function calculateName(name)
                {
                   
                    var myName = "";
                    var countConsonants = 0;
                    for (var i = 0; i < name.length; i++)
                    {
    // after here was: if ( (name[i] != "a") && (name[i] != "e") && (name[i] != "i")  && (name[i] != "o") && (name[i] != "u") 
    
    /*And now that is*/ if ( name[i] != vocals ) 
                        {
                            myName = myName + name[i];
                            myName = myName.substring (0,3);
                            countConsonants ++;
                        }
                    }

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    if ( name[i] != vocals )
    That compares a string with a function address, which doesn't begin to make sense.

    Code:
                function vocals ()
                {
                    var v;
                    v = "a" + "e" + "i" + "o" + "u";
                }
    That function (even if you call it properly) has no effect. It does no useful processing and returns nothing.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by triko View Post
    HI all!
    I have this code, but when I running, the program ignore the function vocals and it run without remove the vocals!!! Can you fund my error?
    Thanks All
    Code:
                function vocals ()
                {
                    var v;
                    v = "a" + "e" + "i" + "o" + "u";
                }
                
                function calculateName(name)
                {
                   
                    var myName = "";
                    var countConsonants = 0;
                    for (var i = 0; i < name.length; i++)
                    {
    // after here was: if ( (name[i] != "a") && (name[i] != "e") && (name[i] != "i")  && (name[i] != "o") && (name[i] != "u") 
    
    /*And now that is*/ if ( name[i] != vocals ) 
                        {
                            myName = myName + name[i];
                            myName = myName.substring (0,3);
                            countConsonants ++;
                        }
                    }
    vocals() is different from vocals.

    Regardless, you need to understand how functions work. Just now, vocals() doesn't return anything. All it does, is set a variable inside itself. So, doing name[i] != vocals() doesn't do anything (that's not entirely true, it checks if name[i] will equal the return from vocals() but because there's no return specified, nothing is returned, and your if will always evaluate true).

    I don't know why you're wrapping your list of vowels in a function anyway tbh, it's not needed. You could just define the list above the for loop and it'll do an identical job. Functions are about reusable code - if you only need to do something once, don't make it a function.

    On top of this, v in vocals will equal "aeiou", so when you do name[i] = v, you're comparing a single character in name with aeiou, which will always be false. You need to approach that differently.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Hm ... there are several things that make me think that you are still in a very basic learning phase.

    - vocals is a function. To call the function, you need parentheses () vocals(). To compare the result of a function to some other value, you must return something from the function
    - What exactly do you want to achieve? Remove the vocals? So you want to find out if name[i] is a vocal?
    Code:
    function vocal(character) {
       var returnValue = false;
       if(character == "a" || character == "i" || character == "e" || character == "o" || character == "u")
          returnValue = true;
       return returnValue;
    }
    
    ...
    
    if(!vocal(name[i])) {
       // here the current character is not a vocal
    }
    Last edited by devnull69; 11-12-2012 at 04:26 PM.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,484
    Thanks
    23
    Thanked 636 Times in 635 Posts
    I'm sure RegEx would make this a snap, but they take me all day to work out so I used a loop and
    Code:
    if (name[i] == 'a' || name[i]  == 'e' || name[i] == 'i' || name[i] == 'o' || name[i] == 'u')
    This does lead to some strange results if you remove the vowel from the string. So I ended up substituting for the vowels and then removing the token. It took two loops:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script type="text/javascript">
    function calculateName(name)
    {
    	var myName = "";
    	var countConsonants = 0;
    	var count = name.length;
    	for (var i = 0; i < count; i++)
    	{
    		if (name[i] == 'a' || name[i]  == 'e' || name[i] == 'i' || name[i] == 'o' || name[i] == 'u')
    		{
    			name = name.replace(name[i], '~');
    		}
    	}
    	for (var i = 0; i < count; i++)
    	{
    		name = name.replace('~', '');
    	}
    	document.getElementById('here').innerHTML = name;
    }
    </script>
    </head>
    <body>
    <button onclick="calculateName('BillyJoeBoo')">PUSH</button>
    <div id="here"></div>
    </body>
    </html>

  • #6
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by sunfighter View Post
    I'm sure RegEx would make this a snap
    Slightly simpler but easily avoided for a beginner. I suspect the original intention may have been to build a string from the extracted vowels, otherwise counting the consonants would be unnecessary.
    Code:
    <!DOCTYPE html >
    <html>
    <head>
    <title>TEST</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    
    function calculateName( str )
    {
      var vowelString = "", ch;
      
      for( var i = 0; (ch = str.charAt( i )); i++ )
        if( "aeiouAEIOU".indexOf( ch ) > -1 )
          vowelString += ch;
      
      document.getElementById('here').innerHTML = vowelString;
      
      return str.length - vowelString.length;
    }
    
    </script>
    </head>
    <body>
    <button onclick="alert('Consonants: '+calculateName('BillyJoeBoo'))" >PUSH</button>
    <div id="here"></div>
    </body>
    </html>

  • #7
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    I don't know why you're wrapping your list of vowels in a function anyway tbh, it's not needed. You could just define the list above the for loop and it'll do an identical job. Functions are about reusable code - if you only need to do something once, don't make it a function.
    I would put the list of vowels into function because I reuse this in other point of program !!!
    But i don't know how to do!!!

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Did you even read my response?

  • #9
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by triko View Post
    I would put the list of vowels into function because I reuse this in other point of program !!!
    But i don't know how to do!!!
    The best way is to assign it to a variable, then pass the variable list into functions in the future, rather than calling the function inside it. Much more efficient - less operations and less memory.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #10
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Did you even read my response?
    Yes now I read, sorry!!!
    You understand me.. I have low experience with Javascript, and the method that you reccomended it's for me

  • #11
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    The best way is to assign it to a variable, then pass the variable list into functions in the future, rather than calling the function inside it. Much more efficient - less operations and less memory.
    Yes blue, but I must use this function many times in the program

  • #12
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    Slightly simpler but easily avoided for a beginner. I suspect the original intention may have been to build a string from the extracted vowels, otherwise counting the consonants would be unnecessary.
    Code:
    <!DOCTYPE html >
    <html>
    <head>
    <title>TEST</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    
    function calculateName( str )
    {
      var vowelString = "", ch;
      
      for( var i = 0; (ch = str.charAt( i )); i++ )
        if( "aeiouAEIOU".indexOf( ch ) > -1 )
          vowelString += ch;
      
      document.getElementById('here').innerHTML = vowelString;
      
      return str.length - vowelString.length;
    }
    
    </script>
    </head>
    <body>
    <button onclick="alert('Consonants: '+calculateName('BillyJoeBoo'))" >PUSH</button>
    <div id="here"></div>
    </body>
    </html>
    Logic, i'm a low level in Javascript and what that you write it's more complicately for me
    And don't understand more passages of problem

  • #13
    New Coder
    Join Date
    Oct 2012
    Location
    Italy
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for all Helping, I complete the first part of program
    Please test! If you have advice for me, please post
    Code:
    <!DOCTYPE HTML>
        <html>
            <head>
                <title>Tax Code</title>
                <meta charset = "UTF-8" lang= "en, it" />
                <script type="text/javascript">
                   
                function consonants (consonant)
                {
                    var returnValueConsonant = false;
                    if (consonant == "b" || consonant == "c" || consonant == "d" || consonant == "f" || consonant == "g" || consonant == "h" || consonant == "i" || consonant == "j" || consonant == "k" || consonant == "l" || consonant == "m" || consonant == "n" || consonant == "p" || consonant == "q" || consonant == "r" || consonant == "s" || consonant =="t" || consonant == "v" || consonant == "w" || consonant == "x" || consonant == "y" || consonant == "z")
                    {
                        returnValueConsonant = true;
                    }
                    return returnValueConsonant;
                }
                
                function vowels (vowel)
                {
                    var returnValueVowel = false;
                    if (vowel == "a" || vowel == "i" || vowel == "e" || vowel == "o" || vowel == "u")
                    {
                        returnValueVowel = true;    
                    }
                    return returnValueVowel;
                }
                
                function calculateName(name)
                {
                   
                    var myName = "";
                    var countConsonants = 0;
                    for (var i = 0; i < name.length; i++)
                    {
                        
                        if ( !vowels (name[i]) )
                        {
                            myName = myName + name[i];
                            myName = myName.substring (0,3);
                            countConsonants ++;
                        }
                    }
                    if (i == 1)
                    {
                        myName = myName + "XX";
                        var nameReturn = name + myName;
                    }
                    if (i == 2)
                    {
                        myName = myName + "X";
                        var nameReturn = name + myName;
                    }
                    if (countConsonants == 3)
                    {
                        var nameReturn = myName;
                    }
                    if (countConsonants < 3)
                    {
                        var emanYm= "";
                        for (var i = 0; i < name.length; i++)
                        {
                            if ( !consonants (name[i]) )
                            {
                                emanYm = emanYm + name[i];
                                emanYm = emanYm.substring (0,1);   
                            }
                        }
                        myName = myName + emanYm;
                        nameReturn = myName;
                    }
                    if (countConsonants > 3)
                    
                    {
                        var eman= "";
                        for (var i = 0; i < name.length; i++)
                        {
                            if ( (name[i] != "a") && (name[i] != "e") && (name[i] != "i")  && (name[i] != "o") && (name[i] != "u") )
                            {
                                eman = eman + name[i];
                                eman = eman.substring (0,4);   
                            }
                        }
                        name = eman.charAt(0) + eman.charAt(2) + eman.charAt(3);
                        var nameReturn = name;
                    } 
                    alert (nameReturn.toUpperCase() ) 
                    alert (countConsonants);
                }
                
                function calculateSurname(surname)
                {
                   
                    var mySurname = "";
                    var countConsonants = 0;
                    for (var i = 0; i < surname.length; i++)
                    {
                        
                        if ( (surname[i] != "a") && (surname[i] != "e") && (surname[i] != "i")  && (surname[i] != "o") && (surname[i] != "u") )
                        {
                            mySurname = mySurname + surname[i];
                            mySurname = mySurname.substring (0,3);
                            countConsonants ++;
                        }
                    }
                    if (countConsonants == 1)
                    {
                        mySurname = mySurname + "XX";
                        var surnameReturn = mySurname;
                    }
                    if (countConsonants == 2)
                    {
                        mySurname = mySurname + "X";
                        var surnameReturn = mySurname;
                    }
                    if (countConsonants > 2)
                    {
                        var emanrus= "";
                        for (var i = 0; i < surname.length; i++)
                        {
                            if ( (surname[i] != "a") && (surname[i] != "e") && (surname[i] != "i")  && (surname[i] != "o") && (surname[i] != "u") )
                            {
                                emanrus = emanrus + surname[i];
                                emanrus = emanrus.substring (0,3);   
                            }
                        }
                        var surnameReturn = emanrus;
                    }
                    alert (surnameReturn.toUpperCase() );
                    alert (countConsonants);
                }
                function calculation()
                {
             //Create var, for take a data to input
                var name =      document.getElementById("myName").value;
                var surname =   document.getElementById("mySurname").value;
    
    
             //With value of var I do the calculation of Tax Code
                var txName = calculateName(name);
                var txSurname = calculateSurname(surname);
                }
                </script>
               
            </head>
            
            <body>
                Name:    <input type = "text" id = "myName" /> <br />
                Surname: <input type = "text" id = "mySurname" /> <br />
                         <button type = "button" onclick = "calculation()"> CONFIRM </button>
                
                
            </body>
        </html>

  • #14
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
      function vowels (vowel)
                {
                    var returnValueVowel = false;
                    if (vowel == "a" || vowel == "i" || vowel == "e" || vowel == "o" || vowel == "u")
                    {
                        returnValueVowel = true;    
                    }
                    return returnValueVowel;
                }
    Use the indexOf method:
    Code:
    function vowels (vowel)
    {
      return "AEIOUaeiou".indexOf( vowel ) > -1;
    }
    I'm sure you can guess how to write the consonants function.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,695
    Thanks
    80
    Thanked 4,511 Times in 4,475 Posts
    I may get shot for this, but...

    Code:
    function calculateName(name)
    {
        return  (name.toUpperCase() + "XXX" ).replace( /[aeiou]/ig, "" ).substring(0,3);
    }
    Presto. Done.

    If you want to keep the first letter, even if it is a vowel, then:
    Code:
    function calculateName(name)
    {
       return (name.charAt(0) + (name + "XXX").substring(1).replace(/[aeiou]/ig,"") ).substring(0,3).toUpperCase();
    }
    Totally untested. Off the top of my head. Shall I go test it?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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