【问题标题】:Second parameter of a functino called in a conditional?在条件中调用的函数的第二个参数?
【发布时间】:2018-08-26 18:02:48
【问题描述】:

我正在尝试创建一个函数来处理一系列问题和一系列答案。那么我所要做的就是调用函数,每次调整参数。

let playerScore = 0;
let questions = ['What is the capital of Germany?', 'Who is coolest man 
in Bloominton, IL?'];
let answers = ['BERLIN', 'VANCE'];

let quiz = (questions, answers) => {
  let quizAnswer = prompt(questions);
   if (quizAnswer.toUpperCase === answers) {
    playerScore++;
    alert('That is correct!');
  } else {
     alert("I'm sorry, but that is not correct");
   };
 };

quiz(questions[0], answers[0]);

条件无法正确运行。变量 'answers' 正确调用,直到它位于条件语句内。我假设它与范围有关。有没有办法解决这个问题?

【问题讨论】:

  • toUppercase() 缺少 (),首先。
  • 哇。谢谢!我显然是这方面的初学者。我绞尽脑汁想弄清楚这一点。

标签: javascript function conditional


【解决方案1】:

toUpperCase() 是一个函数,而不是一个属性。

以下更改 answerquestion 为单数,以免与数组名称混淆

let playerScore = 0;
let questions = ['What is the capital of Germany?', 'Who is coolest man in Bloominton, IL?'];
let answers = ['BERLIN', 'VANCE'];

let quiz = (question, answer) => {
  let quizAnswer = prompt(question);
   if (quizAnswer.toUpperCase() === answer) {
    playerScore++;
    alert('That is correct!');
  } else {
     alert("I'm sorry, but that is not correct");
   };
 };

quiz(questions[0], answers[0]);

【讨论】:

  • prompt() 应检查返回值(如果用户选择取消,则返回 null)。
猜你喜欢
  • 2017-12-12
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多