Reputation: 11
My question is potentially embarrassing. I'd consult google if I knew what question to ask specifically, but alas, I'm a code newbie and as such am not familiar with the jargon...
I'm following a written tutorial @https://javascript.info. I'm on the lesson about conditional operators and if statements, having trouble wrapping my head around the behaviour of one of the tasks (http://javascript.info/ifelse#tasks), specifically task #4. "Check the login".
Here's the code:
let userLogin = prompt("Who's there?", "");
if (userLogin == 'Admin') {
let pass = prompt("Password?", ""); // *******
if (pass == 'TheMaster') {
alert('Welcome!');
} else if (!pass) {
alert("Canceled.");
} else {
alert('I do not know you');
}
} else if (!userLogin) {
alert("Canceled");
} else {
alert("I don't know you")
}
My question revolves around the (****) line. The code doesn't work properly if that line isn't nested in the 'if', which threw me way off ( had it as a "global" variable to begin with, had to check the solution because I couldnt find the bug ).
I ask of you, please, clarify why that is so. :(
edit I didn't realise I was being unspecific. I'm completely new to the forum, won't happen again.
The code doesn't work, meaning writing 'Admin' into the first prompt isn't necessary to proceed to the 'pass' prompt. This behaviour only happens when I don't nest the "pass" declaration inside 'if'. I think I have found my answer, but wanted to edit as to not further agitate the community. :P
let userLogin = prompt("Who's there?", "");
let pass = prompt("Password?", ""); // *******
if (userLogin == 'Admin') {
if (pass == 'TheMaster') {
alert('Welcome!');
} else if (!pass) {
alert("Canceled.");
} else {
alert('I do not know you');
}
} else if (!userLogin) {
alert("Canceled");
} else {
alert("I don't know you")
}
This is what I mean - hope this clarifies! Sorry again.
Upvotes: 0
Views: 88
Reputation: 139
The only difference I found is that if you remove the line (*) from the if block the script will ask the user for the **userLogin and the pass, and then do the checking. If you do not, the script will ask for the userLogin and if this is not correct, you will not waste time asking for the pass. As the others say, it would be better to explain what problem you have exactly.
Upvotes: 0
Reputation: 2658
my guess is that you had the pass either in a separate nested scope, or declared after it was being used. let
declarations are not hoisted
like var
declarations. So they must be accessed from the same scope, or a more nested scope of that scope, AFTER their declaration.
let userLogin = prompt("Who's there?", "");
let pass = prompt("Password?", ""); // *******
if (userLogin == 'Admin') {
if (pass == 'TheMaster') {
alert('Welcome!');
} else if (!pass) {
alert("Canceled.");
} else {
alert('I do not know you');
}
} else if (!userLogin) {
alert("Canceled");
} else {
alert("I don't know you")
}
a link to definition of hoisting if you're curious : https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
PS - I hope this is just a toy problem... but incase it isn't please do not use this to perform actual authentication. Anything which is in the Javascript of a page is visible to all and your username and password will NOT be secure.
Upvotes: 1