Java脚本))关于随机问答游戏的问题

时间:2019-03-10 20:36:57

标签: javascript

我正在制作一个简单的随机问答游戏

我为游戏编写了一些脚本

var Playerfirstname = prompt("Enter your first name");

var Playerlastname = prompt("Enter your last name");
alert(`Hello ${Playerfirstname} ${Playerlastname}!`);

console.log("Player name is :",Playerfirstname +","+ Playerlastname);

 var round1quiz = [
     ['Whats is 2 + 2', '4'],
     ['What is 3 * 3', '9'],
     ['What is 5 * 5', '25']
 ];

 var round2quiz = [
     ['Whats my name', 'Ian'],
     ['Where am i from', 'India'],
     ['My favorite Food', 'Idly']
 ];

  var round3quiz = [
     ['Whats my name', 'Ian'],
     ['Where am i from', 'India'],
     ['My favorite Food', 'Idly']
 ];
 
 score = 0;
 var questions = 0;

function round1()
{

 shuffle(round1quiz)
 var round1 = prompt("If you want to start Quiz game, enter 'yes'");

 if (round1 == 'yes' || round1 == 'y')
 {
   alert("Let's start Quiz game!");
   alert("Round 1");
   questions = round1quiz;

 }
 else
 {
   alert("sorry, try again");
   var round1 = prompt("If you want to start Quiz game, enter 'yes' or 'y' ");
 }


}

round1();



function round2()
{


}



 function randomQuestions() {
     return [rq(), rq(), rq()]
 }

 function rq() {
     var a = getRandomInt(0, 100),
         b = getRandomInt(0, 100),
         operator = "+-*" [getRandomInt(0, 3)],
         answer = operator === "+" ? a + b : operator === "-" ? a - b : operator === "*" ? a * b:0;



     return ["what is " + a + operator + b, answer]
 }

 function getRandomInt(min, max) {
     return Math.floor(Math.random() * (max - min)) + min;
 }

 function askQ(ans) {


     var answer = prompt(ans[0], '');
     if (answer == ans[1]) {
         score++;
         alert('Your are right!, you get money');
     } else {
         alert('Sorry, It is wrong answer');
         
     }
 }



 // the loop that will ask all the questionseasy

 function startquiz() {
     for (var i = 0; i < questions.length; i++) {
         askQ(questions[i]);

     }
 }

 startquiz();
 alert(score);

 function shuffle(array) { // 
     var currentIndex = array.length,
         temporaryValue, randomIndex;

     while (0 !== currentIndex) {

         
         randomIndex = Math.floor(Math.random() * currentIndex);
         currentIndex -= 1;

         
         temporaryValue = array[currentIndex];
         array[currentIndex] = array[randomIndex];
         array[randomIndex] = temporaryValue;
     }

     return array;
 }

我想在代码中放入round2和round3

如果玩家输入了所有正确答案并选择播放第二回合,则会显示第二回合问题。

但是,如果玩家选择停止玩游戏,则游戏将结束 如何将这些代码放在脚本中?

1 个答案:

答案 0 :(得分:1)

第一件事:如果您打算学习JS的基本语法(或任何编程语言的基础知识),请继续。

您的代码有一些错误和错误的概念。由于任何其他原因(成为webdeveloper / node.js等),请立即停止并首先了解这些基础知识。 (“了解HTML,CSS以及JS与它们的交互方式”或“如何在机器和第一个“ hello world”程序上设置节点服务器”)

要回答您的问题:您的问题很难回答,一团糟。但是基本上,您需要在每个回合开始时询问用户是否要继续。

将每个回合保存到数组中...

var rounds = [round1, round2, round3];

...之后再打一次回合...

var currentRound;
while (currentRound = rounds.pop()) {
    currentRound();
}

会发生什么:我们将函数自身保存到变量中,然后调用它们。

还有很多改进的方法,但是您可以从此开始。