首页 > 解决方案 > 节点脚本类型错误:无法读取未定义的属性“作者”

问题描述

我试图编写代码来构建测验应用程序。但我在这里面临错误。由于我是编码新手,因此无法详细说明问题。我想请您在您的系统中运行此代码。并建议我合适的答案。我在这部分发现错误let userAnswer = read.keyInSelect(choice, question);还说:

ReferenceError: readlineSync is not defined
    at quiz (/home/runner/AssignmentQuiz/index.js:39:20)
    at /home/runner/AssignmentQuiz/index.js:57:1
    at Script.runInContext (vm.js:130:18)
    at Object.<anonymous> (/run_dir/interp.js:209:20)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47Hint: hit control+c anytime to enter REPL.

这是代码,

const read = require('readline-sync');
const chalk = require('chalk');
const print = console.log;

//Get Name from User
let usrName = read.question(chalk.bgCyanBright.black(' Enter your name :  \t'));

print("\n\n");
print(chalk.bgRed.white.bold(`\t\t\t\t\t\t Welcome ${usrName} to The Quiz Game!!!  \n`));
print("\n");

//Printint Game Rules
print(chalk.underline.green.bold("Rules:\n"));
print(chalk.bgGreen.black.bold(" 1. ") + (" You Should Be Older Than 13+."));
print(chalk.bgGreen.black.bold(" 2. ") + (" It\'s Compulsory You Have To Attedend Every Question."));
print(chalk.bgGreen.black.bold(" 3. ") + (" Each Question Carries 5 Marks."));
print(chalk.bgGreen.black.bold(" 4. ") + (" On Every Wrong Question 2 Marks Will Be Deducted."));
print(chalk.bgGreen.black.bold(" 5. ") + (" You Will Atleast Get 4 Options To Choose Answer From."));
print(chalk.bgGreen.black.bold(" 6. ") + (" You  Will Get Only 1 Attempt To Give Right Answer."));

//Get Age from User
print("\n");
let usrAge = read.question(chalk.bgCyanBright.black(` Hey ${usrName} how old are you :  \t\t`));
print("\n\n");

let score = 0;
let rightAnswer=0;
let mcq = [
  {
    choice: chalk.red['query', 'programming', 'spoken', 'scripting'],
    question: '1. javascript is called _____________ language? \nAnswer: ',
    answer: 'scripting',
  },
  {
    choice: chalk.red['yes', 'no', 'may be', 'not sure'],
    question: '2. Do Java and JavaScript the same language? \nAnswer: ',
    answer: 'no',
  },
  {
    choice: chalk.red['c', 'python', 'swift', 'java'],
    question: '3. Name \'JavaScript\' is inspired by _____________________ language? \nAnswer: ',
    answer: 'java',
  },
  {
    choice: chalk.red['plus plus', 'modulus', 'increment', 'decrement'],
    question: '4. \'++\'is ______________________ operator called? \nAnswer: ',
    answer: 'increment',
  },
  {
    choice: chalk.red['client', 'server', 'front-end', 'backend'],
    question: '5. \'JavaScript\' is also known as _____________-side scripting language? \nAnswer: ',
    answer: 'client',
  }
];
function check(choice, question, answer) {
  let userAnswer = read.keyInSelect(choice, question);
  print('\n');
  if (choice[userAnswer] === answer) {
    print(`${usrName} your answer is correct !`);
    score += 5;
    rightAnswer++;
  }
  else {
    print(`Wrong answer! ${usrName}`);
    print(`Right answer is: ${answer}`);
    score -= 2;
  }
}

for(let i = 0;i < mcq.length; i++){
  print('\n');
  check(mcq[i].choice,mcq[i].question,mcq[i].answer);
  print('\n---------------------------------------------------\n');
}

if(rightAnswer>=3){
  print(`Congratulations! ${usrName} you have successfully completed the quiz.`);
print(`Your score is : ${score}`);
}
else{
  print('You have lost!');
}

标签: javascriptnode.jsnpmtypeerrornode-modules

解决方案


推荐阅读