Quote:
Originally Posted by triko
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.