0
votes
let isValidPassword = function (password) {
    if (password.length <= 8 && password.includes('password')) {
        console.log('Password is not according to Company Policy.');
    } else {
        console.log('Password is accepatble!');
    }
}
isValidPassword();

Here password.length is not working.

Error is : TypeError: Cannot read property 'length' of undefined at isValidPassword (C:\Users\Gitanshu Choudhary\Desktop\modern_js\hello_earth.js:163:18) at Object. (C:\Users\Gitanshu Choudhary\Desktop\modern_js\hello_earth.js:169:1) at Module._compile (internal/modules/cjs/loader.js:956:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) at Module.load (internal/modules/cjs/loader.js:812:32) at Function.Module._load (internal/modules/cjs/loader.js:724:14) at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) at internal/main/run_main_module.js:17:11

2
You call isValidPassword() without any arguments. Hence, password is undefined. - str
call isValidPassword('you password') - Afia

2 Answers

4
votes

You're also making one mistake here which is &&, you mean here if the password is less than 8 and also must exists the password string in it, it will give you the correct result, otherwise wrong. if both is true, the code will be executed, however it is false.

true && true // true
true && false // false

here is the correct result:

let isValidPassword = function (password) {
    if (password.length <= 8 || password.includes('password') ) {
        console.log('Password is not according to Company Policy.');
    } else {
        console.log('Password is accepatble!');
    }
}
isValidPassword('passwordkksk');

Thanks!

3
votes

You have to pass some parameter while calling the Function.

In your case it should be something like :

isValidPassword('password');

OR using variable

let pass = 'abc@123';
isValidPassword(pass)