做...同时......一个功能

时间:2016-03-13 20:27:48

标签: javascript loops do-while

我想知道如何在javascript中执行一个函数来执行do ... while的过程,但我不知道如何。

    do {
        tableTimes = prompt ("Which number do we do the table times?")
    } while (isNaN(tableTimes) == true);


    do {
        timesByStart = prompt("From which number do we start?")
    } while (isNaN(timesByStart) == true);

    do {
        timesByEnd = prompt("In which number do we end?")
    } while (isNaN(timesByEnd) == true);

这就是我所拥有的,但我希望有类似的东西:

    askTheNumber(nameOfTheVariable, Question)

我该怎么办?它可以吗?

2 个答案:

答案 0 :(得分:2)

这是你在找什么?

function askTheNumber(question) {
  var answer;
  do {
    answer = prompt(question);
  } while (isNaN(answer));
  return answer;
}

var tableTimes=askTheNumber("Which number do we do the table times?");

答案 1 :(得分:0)

此类函数只需要一个参数question。请参阅下面的代码

function askTheNumber(question){
  do {
      var input = prompt(question);
   } while (isNaN(input) == true);
   return input
};


vat timesByStart = askTheNumber("From which number do we start?");
var timesByEnd = askTheNumber("In which number do we end?");
var tableTimes = askTheNumber("In which number do we end?");