FlerrElectronics
FlerrElectronics

Reputation: 291

Re-running javascript code with out having to write it again

I am coding Javascript in Node.js, I have this function.

  function addaccountfunc(usrnew, passnew) {
  console.log(" Forgotten passwords/usernames can NOT be reset!")
  if (usrnew === usr ){
  console.log("Sorry, that username is taken, would you like to chose another?")   

    }
     var usrnew = prompt("Chose a username: ")

     if (passnew != password){
     var passnew = prompt("Chose a password:")
    }


    }

The problem is I would like to keep re running the usrnew prompt, until a username is entered that does not equal something already in the database, how would I go about doing this?

Upvotes: 0

Views: 69

Answers (1)

Trey50Daniel
Trey50Daniel

Reputation: 179

You can always use a while loop:

var usrnew = prompt("Choose a username: ")
while(usrnew === usr) {
    console.log("Sorry, that username is taken, would you like to choose another?")   
    var usrnew = prompt("Choose a username: ")
}

Upvotes: 2

Related Questions