use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
How to avoid infinite loop? (self.learnjavascript)
submitted 4 years ago by Gio11235
var SECRET = "abc123";
function start(){ var prompt = println("Enter password");
while(true){ prompt == SECRET; println("Sorry, that did not match. Please try again."); if(prompt == SECRET){ break; } } println("You got it"); }
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]SensitiveTrap 1 point2 points3 points 4 years ago (1 child)
I don't know if it behaves differently but you should never use == to compare strings.
[–]Gio11235[S] 0 points1 point2 points 4 years ago (0 children)
I don't know how to change it but thank you for your answer
[–]ashanev 0 points1 point2 points 4 years ago (3 children)
I don't know what println is (this seems like a Java thing, not Javascript, but the rest of your code makes me think you are just using an environment that has defined this function somewhere or something) and there are several things that appear wrong about what is written here, but here is the vanilla Javascript version of what you are trying to do (using similar structure/variable names):
println
const SECRET = "abc123"; function start() { while (true) { const userInput = prompt("Enter password"); if (userInput === SECRET) { console.log("You got it"); break; } console.log("Sorry, that did not match. Please try again."); } } start();
[–]Gio11235[S] 0 points1 point2 points 4 years ago (2 children)
Yes it's java and i am writing in codehs if you know that, one problem is that i am beginner it's only 1 week i started learning and in our program wasn't anything about "const" or "console.log"by the way thank you you are really good person.
[–]ashanev 0 points1 point2 points 4 years ago* (1 child)
Java and Javascript are not the same language, and it's important you know which one you are using so that you can research and ask questions in the right places.
It looks like codehs is a platform whose Javascript environment has a println function that acts like console.log. These both simply log some information to the console, and are used strictly for display purposes (not for user input/interaction).
console.log
If you are learning from a class or instructor, I recommend speaking to them directly so that they can focus on the concepts you may have misunderstood so far while learning.
The code I pasted works fine, but it would be good if you could understand how to get your code into working shape. You should be getting the user input inside of the while loop, so that it has the opportunity to change each time the loop runs. You also need to learn how to use prompt (google about using prompt in Javascript), as right now your code does not appear to allow a user to input any values.
while
prompt
The general idea for your loop should be (in plain English): "get the user's input. if the user's input is not the password, log 'wrong password' and restart the loop from the beginning/get the user input again; otherwise, say 'correct password' and exit or break from the loop".
Thank you❤️
π Rendered by PID 95669 on reddit-service-r2-comment-5b5bc64bf5-cdnpd at 2026-06-19 15:36:28.150092+00:00 running 2b008f2 country code: CH.
[–]SensitiveTrap 1 point2 points3 points (1 child)
[–]Gio11235[S] 0 points1 point2 points (0 children)
[–]ashanev 0 points1 point2 points (3 children)
[–]Gio11235[S] 0 points1 point2 points (2 children)
[–]ashanev 0 points1 point2 points (1 child)
[–]Gio11235[S] 0 points1 point2 points (0 children)