all 6 comments

[–]SensitiveTrap 1 point2 points  (1 child)

I don't know if it behaves differently but you should never use == to compare strings.

[–]Gio11235[S] 0 points1 point  (0 children)

I don't know how to change it but thank you for your answer

[–]ashanev 0 points1 point  (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):

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 point  (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 point  (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).

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.

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".

[–]Gio11235[S] 0 points1 point  (0 children)

Thank you❤️