View Single Post
Old 11-12-2012, 03:37 PM   PM User | #3
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote