This week's book giveaway is in the NodeJS forum. We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line! See this thread for details.
This is for a password verifier homework assignment. I need to loop until the password meets the criteria. Right now my program keeps looping even after it accepts the password. I've tried a break statement but then it just wouldn't loop at all. I also cant seem to figure out how to check if a string contains a digit. if anyone could offer any advice as to what im doing wrong it would be greatly appreciated.
That's actually assigning the value true to validPassword. The result is that this will always be true, and so the loop will never end.
Since validPassword is a boolean, however, you should not be comparing it like that, you just need to use validPassword:
However, if you look at your do-while code, where in there are you changing the value of validPassword?
DT is right. Never write == true nor == false which are both poor style and error‑prone, as you have seen.
Never while (b == true) ... Always while (b) ... Never while (b == false) ... Always while (!b) ... Never if (b == true) ... Always if (b) ... Never if (b == false) ... Always if (!b) ...
What is more, your loop will continue to run while you have a good password, and will stop when you have a bad password (line 18).
Maybe you should loop through the individual chars in your password and test them with a method of the Character class. Maybe you should try something different . . . .
posted 1 week ago
Ok, thanks for pointing that out. I guess I didn't realize I was assigning the value. that makes sense now, but I still can't seem to get my loop right. Any way I change it, it either stops when it shouldn't or keeps looping when accepted. I'll keep playing around with it. So do you think it would be best to put each test in a separate method? Thanks
Lor Sturuda wrote:So do you think it would be best to put each test in a separate method?
I think it could be a good idea. Right now it looks like you wanted to write a loop to check for digits in the string, but it's getting mixed up with the code which does the other checks. So you haven't written the loop yet. If you wrote a method boolean hasADigit(string) then you could just write that test and you wouldn't have the other tests making trouble.
Knute Snortum wrote:It looks like in line 13 you aren't setting validPassword when you call the method verifyPassword.
Lor probably thinks that setting the validPassword variable inside that method will do that. However it's declared as a local variable inside the method so it doesn't set the value of the variable which is declared at the top of the posted code fragment.
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
global solutions you can do in your home or backyard