JavaScript中的计分函数,只有一个初始提示

JavaScript中的计分函数,只有一个初始提示,javascript,algorithm,function,Javascript,Algorithm,Function,我似乎不知道如何只提示用户选择一个评分算法一次,而不是每次。我知道我希望能够为用户提供initialPrompt选择评分方法,然后提示他们输入要评分的单词。我正在努力解决的问题是将initialPrompt与runProgram函数分开,这样它就不会在每次完成单词评分时一直调用它 以下是我尝试过的: 1.我试图在runProgram函数之外调用initialPrompt,但似乎无法获取intialprompt的返回值,以便在runProgram中使用它。我已尝试将初始提示设置为runProgr

我似乎不知道如何只提示用户选择一个评分算法一次,而不是每次。我知道我希望能够为用户提供
initialPrompt
选择评分方法,然后提示他们输入要评分的单词。我正在努力解决的问题是将
initialPrompt
runProgram
函数分开,这样它就不会在每次完成单词评分时一直调用它

以下是我尝试过的:
1.我试图在
runProgram
函数之外调用
initialPrompt
,但似乎无法获取
intialprompt
的返回值,以便在
runProgram
中使用它。我已尝试将
初始提示设置为
runProgram
中的变量

2.我尝试创建一个新函数,只在
runProgram
返回一个分数后对一个新词进行评分,这样我就不必调用
runProgram
,后者反过来调用
initialPrompt

let word = '';


//code for the initial user prompt which asks them to choose a scoring algorithm.

function initialPrompt() {
  const input = require('readline-sync');
  let info = input.question(`Welcome to the Scrabble score calculator!

Which scoring algorithm would you like to use?

0 - Scrabble: The traditional scoring algorithm.
1 - Simple Score: Each letter is worth 1 point.
2 - Bonus Vowels: Vowels are worth 3 pts, and consonants are 1 pt.

Enter 0, 1, or 2:`)
  return info
}


// Code for runProgram function here:

function runProgram(scoringAlgorithms) {
  //initialPrompt();
  let num = initialPrompt();
  num = (Number(num));
  const readline = require('readline-sync');
  let word = readline.question(
    `Enter a word to score(Typing 'Stop' will end the program): `);
  word = word.toLowerCase();
  //console.log(word);
  //console.log(typeof num);
  //console.log(num===0);


//conditionals

  if (num === 0) {

    //console.log(initialPrompt);
    console.log("Scoring Choice: ", scoringAlgorithms[0].name);
    console.log(word);
    console.log(`Is worth ${scrabbleScoring(word, newPointStructure)} points`);
    runProgram()

  } else if (num === 1) {
    console.log(initialPrompt);
    console.log("Scoring Choice: ", scoringAlgorithms[1].name);
    console.log(word);
    console.log(`Is worth ${simpleScore(word)} points`);
    runProgram()

  } else if (num === 2) {
    console.log(initialPrompt);
    console.log("Scoring Choice: ", scoringAlgorithms[2].name);
    console.log(word);
    console.log(`Is worth ${bonusVowels(word)} points`);
    runProgram()

  } else if (num === 'Stop')
    console.log("Goodbye")
}
下面是运行时的结果

Welcome to the Scrabble score calculator!

Which scoring algorithm would you like to use?
0 - Scrabble: The traditional scoring algorithm.
1 - Simple Score: Each letter is worth 1 point.
2 - Bonus Vowels: Vowels are worth 3 pts, and consonants are 1 pt.

Enter 0, 1, or 2:0
Enter a word to score(Typing 'Stop' will end the program): Programmer
Scoring Choice:  Scrabble Scoring
programmer
Is worth 17 points
Welcome to the Scrabble score calculator!

Which scoring algorithm would you like to use?

0 - Scrabble: The traditional scoring algorithm.
1 - Simple Score: Each letter is worth 1 point.
2 - Bonus Vowels: Vowels are worth 3 pts, and consonants are 1 pt.

Enter 0, 1, or 2:
这就是我希望它看起来的样子:

Welcome to the Scrabble score calculator!

Which scoring algorithm would you like to use?
0 - Scrabble: The traditional scoring algorithm.
1 - Simple Score: Each letter is worth 1 point.
2 - Bonus Vowels: Vowels are worth 3 pts, and consonants are 1 pt.

Enter 0, 1, or 2: 0
Enter a word to score(Typing 'Stop' will end the program): Programmer
Scoring Choice:  Scrabble Scoring (Or whatever scoring algorithm the user chooses) 
Your word is worth: 17 points

Enter a word to score(Typing 'Stop' will end the program):
以下是完整的示例代码:

尝试将评分算法设置为变量,并在runProgram()中检查变量是否已设置。如果设置为,则不调用初始提示

let word = '';
let scoringAlgo = ''

//code for the initial user prompt which asks them to choose a scoring algorithm.

function initialPrompt() {
  const input = require('readline-sync');
  let info = input.question(`Welcome to the Scrabble score calculator!

Which scoring algorithm would you like to use?

0 - Scrabble: The traditional scoring algorithm.
1 - Simple Score: Each letter is worth 1 point.
2 - Bonus Vowels: Vowels are worth 3 pts, and consonants are 1 pt.

Enter 0, 1, or 2:`)
  return info
}


// Code for runProgram function here:

function runProgram(scoringAlgorithms) {
  //initialPrompt();
  if (scoringAlgo == ''){
    let num = initialPrompt();
    num = (Number(num));
  }
  else {
    let num = scoringAlgo;
  }

  const readline = require('readline-sync');
  let word = readline.question(
    `Enter a word to score(Typing 'Stop' will end the program): `);
  word = word.toLowerCase();
  //console.log(word);
  //console.log(typeof num);
  //console.log(num===0);


//conditionals

  if (num === 0) {

    //console.log(initialPrompt);
    console.log("Scoring Choice: ", scoringAlgorithms[0].name);
    console.log(word);
    console.log(`Is worth ${scrabbleScoring(word, newPointStructure)} points`);
    runProgram()

  } else if (num === 1) {
    console.log(initialPrompt);
    console.log("Scoring Choice: ", scoringAlgorithms[1].name);
    console.log(word);
    console.log(`Is worth ${simpleScore(word)} points`);
    runProgram()

  } else if (num === 2) {
    console.log(initialPrompt);
    console.log("Scoring Choice: ", scoringAlgorithms[2].name);
    console.log(word);
    console.log(`Is worth ${bonusVowels(word)} points`);
    runProgram()

  } else if (num === 'Stop')
    console.log("Goodbye")
}

不要使用递归在循环中运行代码,使用
while
循环。您可以使用
break
中断循环

另外,您可以将
word
stop
进行比较,而不是
num

函数运行程序(scoringAlgorithms){
//initialPrompt();
设num=initialPrompt();
num=(Number(num));
const readline=require('readline-sync');
while(true){
让word=readline.question(
`输入要评分的单词(键入“停止”将结束程序):`);
word=word.toLowerCase();
如果(字==“停止”){
console.log(“再见”);
打破
}
如果(num==0){
//console.log(initialPrompt);
log(“评分选择:”,评分算法[0].name);
console.log(word);
log(`Is worth${scrabblescording(word,newPointStructure)}points`);
}else if(num==1){
console.log(initialPrompt);
log(“评分选择:”,评分算法[1].name);
console.log(word);
log(`Is worth${simpleScore(word)}points`);
}else if(num==2){
console.log(initialPrompt);
log(“评分选择:”,评分算法[2]。名称);
console.log(word);
log(`Is-worth${bonusvorels(word)}points`);
}
}

}
欢迎来到堆栈溢出;您的
runProgram
函数似乎只调用
initialPrompt
函数一次,而不是在循环中,因此您的
runProgram
函数必须被多次调用。如果没有看到一段完整的代码来计算它在做什么,我们将无法回答这个问题;请在这里发布一个。谢谢@kaya3:您需要编辑您的问题以包含代码。谢谢@irtazaakram您将在完整的代码链接中看到,我将
scoringAlgorithms
设置为三个数字的数组,这就是我在条件部分中比较的
num
。那部分的工作方式和我期望的一样。我想帮助您找到一种方法,在调用
runProgram
函数时,不要连续调用
initialPrompt