+ 2

Can someone help?

I need a little help on something. So I want this code to display a prompt box and have the user type a strictly 8 letter password. After it accepts the password, I want it to display a button that you can press to view your password, but its doing some things I don't understand. Any help would be appreciated. Thank you. https://code.sololearn.com/Wj08FS8TYDSe/?ref=app

1st Feb 2018, 2:31 AM
jacksonofgames 28
jacksonofgames 28 - avatar
8 Answers
+ 2
Well, instead of declaring the variable password from within the function, you could define it at the start of your code so that you can easily access it from anywhere. So now, your code should look like this: //Making the password var password = prompt("Enter what you want your password to be"); //Accepting the password function type() { console.log(password); if (password.length < 8) { alert("That password is too short"); prompt("Enter what you want your password to be"); } else if (password.length > 8) { alert("That password is too long"); prompt("Enter what you want your password to be"); } else if (password.length = 8) { alert("Your password was accepted"); } } //Making a button to view the password function show() { alert("Your password is " + password); } show(); window.onload = type();
2nd Feb 2018, 12:43 AM
Faisal
Faisal - avatar
+ 3
Wow, thanks!
2nd Feb 2018, 12:45 AM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 2
Thanks a ton!
1st Feb 2018, 9:25 PM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 2
Also, is there a way to link information from one function to another?
1st Feb 2018, 10:50 PM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 2
You can access outside code in individual functions?
2nd Feb 2018, 12:44 AM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 2
You can access variables from outside of functions, as long as they're declared before the function.
2nd Feb 2018, 12:45 AM
Faisal
Faisal - avatar
+ 2
No problem! šŸ˜‰
2nd Feb 2018, 12:46 AM
Faisal
Faisal - avatar
+ 1
I think the problem with your code is that you're using functions within a function, which usually doesn't work too well. Try instead of setting everything within one function and calling it, write this line of code to run the password function once the window loads: window.onload = password(); Next, the if statement and the other function are trying to access the variable password. The problem is that because they're not in the same function as it, the variable cannot be called. What I would suggest is just to move the if statements within the function to get them to recognize the variable. My last suggestion is to change the name of the function password. The reason for this is because it shares the same name as the variable password, which is why the alert box is printing out the contents of the function. Hope this helped!
1st Feb 2018, 3:45 AM
Faisal
Faisal - avatar