Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-12-2012, 03:24 PM   PM User | #1
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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 ++;
                    }
                }
triko is offline   Reply With Quote
Old 11-12-2012, 03:30 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Code:
if ( name[i] != vocals )
That compares a string with a function address, which doesn't begin to make sense.

Quote:
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.
Logic Ali is offline   Reply With Quote
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
Old 11-12-2012, 04:24 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 530 Times in 524 Posts
devnull69 will become famous soon enough
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..
devnull69 is offline   Reply With Quote
Old 11-12-2012, 04:38 PM   PM User | #5
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,364
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
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>
sunfighter is offline   Reply With Quote
Old 11-12-2012, 06:13 PM   PM User | #6
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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>
Logic Ali is offline   Reply With Quote
Old 11-12-2012, 08:15 PM   PM User | #7
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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!!!
triko is offline   Reply With Quote
Old 11-12-2012, 08:23 PM   PM User | #8
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 530 Times in 524 Posts
devnull69 will become famous soon enough
Did you even read my response?
devnull69 is offline   Reply With Quote
Old 11-12-2012, 08:38 PM   PM User | #9
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
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 
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
Old 11-12-2012, 10:31 PM   PM User | #10
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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
triko is offline   Reply With Quote
Old 11-12-2012, 10:32 PM   PM User | #11
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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
triko is offline   Reply With Quote
Old 11-12-2012, 10:34 PM   PM User | #12
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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
triko is offline   Reply With Quote
Old 11-12-2012, 11:06 PM   PM User | #13
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
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>
triko is offline   Reply With Quote
Old 11-13-2012, 12:12 AM   PM User | #14
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
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.
Logic Ali is offline   Reply With Quote
Old 11-13-2012, 01:11 AM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:34 PM.


Advertisement
Log in to turn off these ads.