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 ++;
}
}
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.
// 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.
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..
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>
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.
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!!!
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.
// 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.
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
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.