Anybody want to help with a JavaScript assignment?

Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#1  Edited By proggykins

So I've spent at least 4 hours trying to get this script to work, and I'm at a loss. My Google-fu has failed me. With the following script (in spoiler) I have to " use a loop so that if the user enters incorrect data three times a message box comes up indicating that three is the limit and that they can no longer attempt log ons." I just need an alert box to pop up, I don't need to actually lock the user out. I know this is ridiculous and would never be used in real life, but this is what the professor wants. I'm sorry if this is an easy question, I've honestly learned nothing this entire semester because this guy doesn't seem to like to teach. Any help would really be appreciated.

Avatar image for skytylz
Skytylz

4156

Forum Posts

9

Wiki Points

0

Followers

Reviews: 0

User Lists: 6

#2  Edited By Skytylz

You might try tested, I've seen threads there of people asking for help with code and getting some.  I don't know much Java, not enough to help anyways.

Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#3  Edited By proggykins

Thanks, I'll try those forums as well!

Avatar image for shiftymagician
shiftymagician

2190

Forum Posts

23

Wiki Points

0

Followers

Reviews: 2

User Lists: 3

#4  Edited By shiftymagician

Hint: use the object that you actually sent instead of the word frmRegister, which you haven't defined before using.  Also use Firebug on Firefox to check for yourself that the error starts on the first line of your code. in your validate(form) function.  If not, just check where the error happens using Firebug.  It will tell you which line in your page causes the error.

Edit: Sorry I mean second line in your function.  First line is fine as it is just a variable declaration that looks fine.

Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#5  Edited By proggykins
@ShiftyMagician: I have to use the code that was in the book, and add in a loop ( the var attempts = 0, attempts=attempts++, and the while loop were added in by me, everything else is from the book). I think he would dock my grade if I completely revamped the book's code.

@example1013: I'm not really experienced at all, besides a very little self-taught C++ and Python. This is an intro to web programming class, but the guy's teaching method until now has been to just have us copy some code and leave.
Avatar image for example1013
Example1013

4854

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#6  Edited By Example1013
@proggykins:
 It also looks like your while may be backwards. If I'm reading the definition of while correctly, it only creates the loop if the statement evaluates to true, and since attempts is set to 0 to begin with, there will be no iteration, since the statement that's supposed to loop with while is false at the beginning.
Avatar image for example1013
Example1013

4854

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#7  Edited By Example1013

It would appear that Mozilla really likes JavaScript, because they have a full reference available fo' free, online. This doesn't look all that different from the VBA and Cocoa that I've dabbled in before. Definitely look at using the reference as a source besides just the book, because the online documentation is almost always ridiculously helpful, since it contains full definitions of every aspect of the code.


What are you using to run the JS, btw?
Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#8  Edited By proggykins
@example1013: I've tried making the while statement true, but that ends up giving me an infinite loop or crashing the browser. I have to make it run in IE. I'll check out the reference, thanks!
Avatar image for example1013
Example1013

4854

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#9  Edited By Example1013
@proggykins: Well, on making the while statement true, you need to make the base case (i.e. the first loop) true, but the fourth loop false. I'd suggest using a less-than-or-equal-to statement. Also, I was wondering what kind of editor or compiler you're using, since JavaScript looks like something interesting to dabble in.

EDIT: For instance, instead of saying:

while attempts > 3

I'd say to try:

while attempts: <= 3

That way once the statement is false (i.e. attempts = 4) the loop is broken.
Avatar image for takua108
takua108

1596

Forum Posts

3503

Wiki Points

0

Followers

Reviews: 1

User Lists: 16

#10  Edited By takua108
@proggykins: I didn't look at it too closely, but when you do "attempts = attempts++" that's not doing what you want it to do. To increment a variable, you just do "attempts++;" depending on how JavaScript handles types and stuff, you're probably doing something weird to the attempts variable. Basically you're saying "hey, Attempts, your new value is the fact that you've incremented yourself." Attempts is then like "lolwut?"
Avatar image for example1013
Example1013

4854

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#11  Edited By Example1013
@takua108 said:

" @proggykins: I didn't look at it too closely, but when you do "attempts = attempts++" that's not doing what you want it to do. To increment a variable, you just do "attempts++;" depending on how JavaScript handles types and stuff, you're probably doing something weird to the attempts variable. Basically you're saying "hey, Attempts, your new value is the fact that you've incremented yourself." Attempts is then like "lolwut?" "

This is actually correct. This is a sample code for something else, but it demonstrates what you need to do with attempts:

Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#12  Edited By proggykins
@example1013: When I write while (attempts <=3) { if statements} I get the same alert boxes until while is untrue. I've tried including while (attempts >3) { alert} as well, but that ends up freezing the IE.

@takua108: Thanks, I tried changing the attempts = attempts++ to attempts++, but it doesn't seem to be working still.


Here's the updated code if anybody would like to see it. This one crashes my IE browser.

Avatar image for takua108
takua108

1596

Forum Posts

3503

Wiki Points

0

Followers

Reviews: 1

User Lists: 16

#13  Edited By takua108

Also there's a random </td> in there; not sure if that has anything to do with anything, but I'm working on making a working version of the code you posted regardless. Be back in a few minutes!

Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#14  Edited By proggykins
@takua108: I copied the code directly from the book, including all the weird, random things they put in. There's also a </label> that shouldn't be in there. The code didn't work straight out of the book, so that was another headache for myself and everyone else in the class. I'll try removing those inconsistencies and see what happens.

Thanks for your help!
Avatar image for okari
Okari

167

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#15  Edited By Okari

 The problem is you're declaring the attempts variable inside of the validation function. Every time you click submit you're resetting attempts to zero.  Declare it outside of the function, and it should save the amount of times the user has attempted to submit invalid text.


Avatar image for takua108
takua108

1596

Forum Posts

3503

Wiki Points

0

Followers

Reviews: 1

User Lists: 16

#16  Edited By takua108
@proggykins: Yeah, no problem, I'm almost done. I hate shitty school programming books, been there done that. Also, I'm not sure yet if this is the main problem, but attempts needs to be set up outside of the function, to be global. Otherwise, attempts is reset to 0 every time you call the function.
Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#17  Edited By proggykins
@Okari: Thanks, I declared attempts outside the function. However, whenever I submit the form, it repeats the same alert box three times, then switches over to my while (attempts > 3) loop. I added  break; to that so it is no longer an infinite loop.
Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#18  Edited By proggykins

I think I might have gotten it. It's pretty hobbled together, but it works...kind of. 

Avatar image for example1013
Example1013

4854

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#19  Edited By Example1013
@proggykins: 
Okay, okay, I see the problem with the while expression. A while statement is supposed to be used to create a continuous loop, starting with a true base case, and continuing until the statement is false.
You're actually trying to do the opposite. The while isn't supposed to execute until it's greater than three.

I think what you actually want is to use a for expression. Something like this:

Hold on, I borked the code.
Avatar image for okari
Okari

167

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#20  Edited By Okari

It's a bit too late for me to debug this but from looking at it there's a few things that should be changed. First, attempts should be attempts = 0. It's automatically declared when the page is loaded, so you're really limiting the user to two errors rather than three. Second, replace the while loops with if statements like this:
I can't get the spoiler warning to work so here's the code:

 function validate(){

var error = 0;

if (attempts => 3){

alert('Error Too many submissions');

}

else if (attempts  < 3){

if (username.length < 6){
alert('error');
error++;
}

if (password1 != password2){

alert('error')
error++;
}

if (error){

attempts++;
}
}
}

Hopefully you get the gist of what I'm saying. I'm a bit constrained on time right now so I can't write the code out completely.

Avatar image for karl_boss
Karl_Boss

8020

Forum Posts

132084

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#21  Edited By Karl_Boss

I could barely handle Visual Basic so I'm sure I can help.

Avatar image for karl_boss
Karl_Boss

8020

Forum Posts

132084

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#22  Edited By Karl_Boss
@Skytylz said:
" You might try tested, I've seen threads there of people asking for help with code and getting some.  I don't know much Java, not enough to help anyways. "
There are quite a few  people here who know coding.
Avatar image for isylence
iSylence

372

Forum Posts

12

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#23  Edited By iSylence

This is what you want.



Things I changed: 
  • Put the attempts outside of the function to make it a global variable.
  • Put an if statement at the end so it will only increment once if at any point in the code returnValue was set to False. The way it was originally they could potentially get stuck in the loop after 1 attempt to log in.
  • Send an alert to let the user know how many attempts have been made.

I really shouldn't just give the answers, but whatever.
Avatar image for example1013
Example1013

4854

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#24  Edited By Example1013
@DrFreeman: Sometimes the best way to learn how to write in a language is to just copy others' examples. I know that's how I've learned everything in my admittedly meager experience.
Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#25  Edited By proggykins
@Okari: I appreciate the help. Unfortunately, the professor wants us to use a loop to tell the user they've exceeded their login limit, so I don't think that method would work. Again, thanks for your help.
Avatar image for takua108
takua108

1596

Forum Posts

3503

Wiki Points

0

Followers

Reviews: 1

User Lists: 16

#26  Edited By takua108
@DrFreeman: Was it intentional to put it in an infinite loop once you're "locked out?"
Avatar image for okari
Okari

167

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#27  Edited By Okari
@proggykins:

Huh. That seems kind of redundant. 
Good luck on the class. Programming can be really frustrating at first, but eventually it can become a fun hobby.
Avatar image for isylence
iSylence

372

Forum Posts

12

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#28  Edited By iSylence
@takua108: It was because I was just leaving that part of his code. He said he wanted a while loop which did that. I was going to reply to that and say he just wants an if at the top just below the variables like this:

if(attempts>=3){
alert("oops");
return false;
}
   
I wrote out the code again and was trying to find a nice way to post it on here so that the formatting wouldn't get screwed up when I moved it from Notepad++ but I can't be bothered. I'll just post it messy again.

Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#29  Edited By proggykins
@DrFreeman: Wow, thanks for that. My hacked together version didn't work on my other computer that's running the same version on IE as my school, but this did. I put break; in the while statement so that the loop isn't infinite. 

It looks like this saga is complete. Thanks for your help everyone! You guys are awesome!
Avatar image for isylence
iSylence

372

Forum Posts

12

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#30  Edited By iSylence
@example1013: Yeah I'm the same, when I'm learning a new language I like to have a few examples to go from to see how it differs from others.

@proggykins: I've put two ways on here as I'm sure you've seen. The second is much more elegant and will only tell them "oops" once for every time they click the button having exceeded their max attempts. The former will give them an infinite loop of "oops" which is bad code. If your lecturer wants that he's crazy and you should just submit the second one (with alerts for errors in it, sorry I commented those out).
Avatar image for proggykins
proggykins

151

Forum Posts

46

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#31  Edited By proggykins
@DrFreeman: I noticed the infinite loop haha. Thanks for all the help, I was worried I wasn't going to be able to complete this.
Avatar image for xeiphyer
Xeiphyer

5962

Forum Posts

1193

Wiki Points

0

Followers

Reviews: 0

User Lists: 8

#32  Edited By Xeiphyer

Crap! I just found the thread but its already been solved =/ Next time!