Reputation: 5
At the moment I'm studying functions, and I cannot really find out what's wrong with the following code. No matter what value "password" takes I always get "NICE"
var passValid = function (password) {
return password.length;
};
if (passValid<5){
console.log("ERROR");
}
else{
console.log("NICE");
}
passValid("somevalue");
Upvotes: -1
Views: 95
Reputation: 703
you are missing argument in function call
if (passValid<5)//no argument
if (passValid("passowrd")<5)//pass an argument
passValid("somevalue");//no need for this
or
var passValid = function (password) {
//return password.length;remove this from here
//}; remove this from here
if (password.length<5){
console.log("ERROR");
}
else{
console.log("NICE");
}
}; //add here so now entire is function
passValid("somevalue");
Upvotes: 2
Reputation: 74036
You don't call the function, but try to compare the function itself with a number instead of using the value returned by that function.
In your case passValid
is just a reference to the function. For a value to be returned you must issue a call to it by using passValid( "someValueInHere" );
Try this instead:
var passValid = function (password) {
return password.length;
};
if ( passValid("somevalue") < 5){
console.log("ERROR");
} else {
console.log("NICE");
}
Upvotes: 1
Reputation: 413712
I think you want:
if (passValid("password") < 5) {
console.log("ERROR");
}
else {
console.log("NICE");
}
I'm not sure how to describe why your original code is wrong; it's just sort-of off in the weeds :-)
Upvotes: 5