如何检测无效的提示答案?

How to detect a invalid prompt answer?

我正在努力实现的示例:

var p = prompt("What is your favorite number?", 1, 2, 3, 4);
if (p) {
  function checkForInvalid() {
    const invalid = "invalid"
  }
}
if (p === invalid) {
  alert("Sorry, I don't know that number");
}

或类似的东西。我对 javascript 比较陌生。 我可以使用函数来检查提示中是否未指定数字吗?我不想验证任何东西...

const validNumbers = [1,2,3,4];

var p = prompt("What is your favorite number?", validNumbers[0]);

if (validNumbers.indexOf(Number(p)) == -1) {
  alert("Sorry, I don't know that number");
}