简体   繁体   中英

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?

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: ")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM