+ 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
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();
+ 3
Wow, thanks!
+ 2
Thanks a ton!
+ 2
Also, is there a way to link information from one function to another?
+ 2
You can access outside code in individual functions?
+ 2
You can access variables from outside of functions, as long as they're declared before the function.
+ 2
No problem! š
+ 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!