Javascript Do While loop using a String

I have an assignment for school and I’ve become stuck. They want us to prompt the user for a secret word with a Do While Loop, and it keeps prompting them until they get the word right. I figured it out with a number, but don’t know how to run a loop with a string instead of an integer. I coded an integer version. This is the code I used:

  const MIN = 1;
      const MAX = 10;

      let secret = "javascript";

      let guesses = 0; // for storing the number of guesses
      let hint = ""; // for storing hint
      let number = 0;
      do {
        // get input from user
        let input = prompt(
          `Please enter a number between ${MIN} and ${MAX}` + hint
        );

        // get the integer
        number = parseStr(input);

        // increase the number of guesses
        guesses++;

        // check input number with the secret number provide hint if needed
        if (number > secret) {
          hint = ", and less than " + number;
        } else if (number < secret) {
          hint = ", and greater than " + number;
        } else if (number == secret) {
          alert(`Bravo! you're correct after ${guesses} guess(es).`);
        }
      } while (number != secret);

How do I add this to a string?

Thanks in advance. I’m also not looking for the answer as much as I’m looking for what concept I’m missing that I need to learn or review so I can do this.

1 Like

I’ve updated it to

 <script>
      let word = prompt("Guess the secret word: ");
      let x = "javascript"
      do{
        let word = prompt("Hint the word is this courses programming language: ")
        x="javascript";}
      while(word!=x)
      
    </script>

I did have it as console.log(word) but then the page would just get stuck.

1 Like

Tangential question: The most frustrating part about learning to program is that they show you everything with integers, never even mentioning how to do it with a string, then ask you to do it… Is it like this in all programming courses. We are using ZyBooks, of which I’m thinking of printing out a copy just so I can burn it.

1 Like

to be honest i just learn as i go according to my needs.

and also maybe dont burn the book?

1 Like

It’s a virtual book. I’m just frustrated. I can’t find a single example online of using a do while loop that parses strings. I appreciate your help though.

1 Like

I FIGURED IT OUT Y’ALL!!!

It is var secret="javascript"; var response; do{ response = window.prompt("Guess the secret word. Hint: The language this course uses."); } while(response!=secret); alert("You guessed it!");

Walks away with smug grin I was trying to make this so complicated!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.