重新运行javascript代码而不必再次编写它

时间:2016-03-18 20:52:04

标签: javascript node.js

我正在编写Node.js中的Javascript,我有这个功能。

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


    }

问题是我想继续运行usrnew提示,直到输入的用户名不等于数据库中已有的内容,我将如何进行此操作?

1 个答案:

答案 0 :(得分:2)

您始终可以使用while循环:

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