...

View Full Version : Having difficulty with loops



Jasonp78
03-25-2012, 07:54 PM
Hey everyone! I am pretty new to JS and I am having a problem figuring this loops structure out - any help would be appreciated. I am trying to add a password to an html page via an external JS file. (I know this isn't an ideal solution for securing a page - I am just trying to use this for trying to understand loops.)

What I am trying to do is prompt the user to enter a password before the html page loads. If the password is correct they can enter the page. If they guess wrong the user is looped back to a prompt box to try again. If they fail 3 times they should be told via an alert box and sent off somewhere else.

I have tried using a for & do while loop and was unsuccessful. I think this is the best way to go.

This is what I have so far:



var password = prompt('All women are:',' ');
var pass1 = "Psycho";
var counter = 0;


if (counter < 3) {
if (password = pass1) {
alert('Password Correct! Click OK to enter!');
counter++
}
else {
alert("Password Incorrect!!!");
window.location="Lab5Part2.html";
}
}
else {

alert("You have failed 3 times!");
window.location="http://www.google.com";
}

Jasonp78
03-25-2012, 08:30 PM
This is the for loop I came up with and the code seems to work. I have been playing with trying to add an else if statement to make it redirect the user to another side after 3 login attempts...but no luck.



var password;
var pass1="Psycho";

password=prompt('All women are:',' ');

for (var count=0;count < 3;count++) {

if (password==pass1) {

alert('Password Correct! Click OK to enter!');
break;
}

else {

alert("Password Incorrect!");
window.location="http://www.google.com";
break;
}
}

Philip M
03-25-2012, 11:14 PM
<html>
<head>
<body>

<script type = "text/javascript">

function getPassword() {

var pass1="Psycho";

for (var count=0;count < 3;count++) {
var password=prompt('All women are:',' ');

if (password==pass1) {
alert ('Password Correct! Click OK to enter!');
// enter the site
window.location.href = "http://www.javascriptkit.com";
return false;
}
else {
if (count <2) {
alert ("You have " + (2-count) + " tries left");
}
}

}

alert ("Password Incorrect After 3 Tries!");
window.location.href ="http://www.google.com";

}

</script>

</body>
</html>

I expect you realise that anyone can see the password simply with View Source.


He could have done one of three things - he didn't do either. Commentator ITV4

Jasonp78
03-26-2012, 01:59 AM
Phillip ty very much for the quick response. I would never use this as security on a live site - would probably have to go with PHP for that. I have tried plugging that in to a webpage and it's not working. I see I forgot to add an if/else with the counter < 2. I have no clue what return false does but I am looking that up momentarily.

My goal was to put the code in an external js file and link it to the head section so the code will execute before the page loads. I am still a little confused on how to do that with a function. Your post gave me a good clue on what I am doing wrong. Thanks so much!

Jasonp78
03-26-2012, 02:08 AM
edit

I used the
<body onload="javascript:getPassword()"> and got the page to function

The return false put the page in an infinite loop which was pretty cool--good to know

Now I have this:



function getPassword() {

var pass1="Psycho";

for (var count=0;count < 3;count++) {
var password=prompt('All women are:',' ');

if (password==pass1) {
alert ('Password Correct! Click OK to enter!');
window.location.href = "test.html";
break;

}
else {
if (count <2) {
alert ("You have " + (2-count) + " tries left");
}
}

}

alert("Password Incorrect After 3 Tries!");
window.location.href ="http://www.google.com";

}


For some reason it works great in Firefox but sends me too google in IE

ps.. the
alert ("You have " + (2-count) + " tries left"); is awesome

Jasonp78
03-26-2012, 07:05 AM
thanx Howard I'd prefer to be pointed in the right direction.... but I have no clue what that jpeg is

Jasonp78
03-27-2012, 07:40 AM
Just wanted to thank ya'll that helped me out. I had to scratch my head a few times but it works in IE and Firefox. Here's what I came up with:



function getPassword() {

var pass1="Psycho";

for (var count=0;count < 3;count++) {
var password=prompt('All women are:',' ');

if (password != pass1) {
if (count < 2) {
alert ("You have " + (2-count) + " tries left");
} else {
alert("Password Incorrect After 3 Tries!");
window.location.href ="http://www.google.com";

}
} else {
password==pass1
alert ('Password Correct! Click OK to enter!');
break;

}
}
}

Jasonp78
03-27-2012, 07:52 AM
Just out of curiousity would there be a way to have them redirected to google when the cancel button is hit?

Old Pedant
03-27-2012, 08:11 AM
Why not experiment, instead of asking?

What happens if you do

var password=prompt('All women are:',' ');
and the person hits cancel? What is the value that comes back in password?

Of course, the question really should be

var password=prompt('All men who think women are psycho are:', ');
if ( password == "Mentally Deficient" ) ...

Mishu
03-27-2012, 08:13 AM
Just out of curiousity would there be a way to have them redirected to google when the cancel button is hit?

Check if the returned value from the prompt is null and if it is, set window.location.href to the redirection url.

Jasonp78
03-27-2012, 08:28 AM
Old Pedant I tried a few things but no luck as of yet...btw thanks for the laugh

Mishu I'll give it a shot

thank ya'll

Old Pedant
03-27-2012, 09:04 AM
As mishu said, hitting cancel causes prompt to return null.

You check for that by doing


var password=prompt('All men who think women are psycho are:','');
if ( password == null )
{
... whatever ...
}
... other code ...

Jasonp78
03-27-2012, 09:50 AM
The part that's getting me confused is the nesting. I spent more time trying to figure out where the curly brackets go and if I have enough curly brackets lol. Is it possible to check if password is null outside of the function or is it necessary to have it nested?

Old Pedant
03-27-2012, 11:20 PM
??? How can you check outside the function if the prompt is *IN* the function??

I guess you *could* return null from the function, but I don't see how that makes the code less complex.

You know, if you properly INDENT your code, it helps a *LOT* in keeping track of the braces.

Any time I see code such as


}
}
}
I *know* that the code is going to be hard to read.

So:


function getPassword()
{
var pass1="Delusional"

for (var count=0;count < 3;count++)
{
var password=prompt('All men who think all women are psycho are:','');
if ( password == null )
{
location.href = "http://www.google.com";
return; // not really needed, but conceptually cleaner
}
if (password != pass1)
{
if (count < 2)
{
alert ("You have " + (2-count) + " tries left");
} else {
alert("Password Incorrect After 3 Tries!");
window.location.href ="http://www.google.com";
return; // again, not needed
}
} else {
alert ('Password Correct! Click OK to enter!');
return;
}
} // end of the for loop

} // end of the function

Old Pedant
03-27-2012, 11:27 PM
Personally, I would have done that more like this:


function getPassword()
{
var pass1="Delusional"

for (var trynum=1; trynum <= 3; ++trynum)
{
var password=prompt("Try number " + trynum
+ "\nAll men who think all women are psycho are:','');
if ( password == null )
{
break; // exit the loop (could also do this by trynum = 4 !)
}
if (password == pass1)
{
alert ('Password Correct! Click OK to enter!');
return;
}
if (trynum < 3)
{
alert ("You have " + (3-trynum) + " tries left");
}
} // end of the for loop

// either null from prompt or 3 tries exhausted
location.href = "http://www.google.com";
return; // not really needed, of course

} // end of the function

See? Much less nesting.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum