我目前正在制作STEM数学游戏。我做了一个函数调用onLoad()
,它获得了20个不重复的随机数,并将它们设置为变量。现在我正在尝试使用该数字并将Q
添加到它的前面,因为我有不同的变量名为Q1
,Q2
等。但是当我尝试console.log时结合了它显示的两个变量X的变量X是Q1
而不是变量Q1
中的字符串。任何帮助将不胜感激!
//Questions - Answers
var Q1 = "5 x 10<sup>7</sup> = ?";
//var A1 = "A: 50,000,000";
var A1 = "<button id='answerButton' type='button' class='reg'>A: 50,000,000</button> <button id='answerButton' type='button' class='reg'>B: 0.0000005</button><br /><br /><button id='answerButton' type='button' class='reg'>C: 5,000,000</button><button id='answerButton' type='button' class='reg'>D: 500,000</button>";
var Q2 = "3.868 x 10<sup>9</sup> = ?";
var A2 = "B: 3,868,000,000";
var Q3 = "What is the answer for 20000000 × 3.5 × 1000000 in scientific notation?";
var A3 = "A: 7 × 10<sup>13</sup>";
var Q4 = "123,000 = ?";
var A4 = "B: 1.23 x 10<sup>5</sup>";
var Q5 = "0.00123 x 10<sup>5</sup> = ?";
var A5 = "A: 12.3 x 10<sup>1</sup>";
var Q6 = "0.0686 x 10<sup>0</sup> = ?";
var A6 = "C: 0.0686";
var Q7 = "2.26 x 10<sup>5</sup> = ?";
var A7 = "B: 226,000";
var Q8 = "0.0002395 = ?";
var A8 = "C: 2.395 x 10<sup>-4</sup>";
var Q9 = "731,000 = ?";
var A9 = "A: 7.31 x 10<sup>5</sup>";
var Q10 = "0.00018 = ?";
var A10 = "B: 1.8 x 10<sup>-4</sup>";
var Q11 = "2x = 54";
var A11 = "D: x = 27";
var Q12 = "x = 81";
var A12 = "A: x = 81";
var Q13 = "4x+8=7.2+5x";
var A13 = "C: x = 0.800";
var Q14 = "4(0.5f−0.25)=6+f";
var A14 = "C: f = 7";
var Q15 = "2+6g=11−3g";
var A15 = "B: g = 1";
var Q16 = "−11f=7(1−2f)+5";
var A16 = "A: f = 4";
var Q17 = "0.5s+1=7+4.5s";
var A17 = "B: s = -1.5";
var Q18 = "2(x+3) = 16";
var A18 = "D: x = 5";
var Q19 = "21+3j = 6";
var A19 = "C: j = -5";
var Q20 = "4(x-1) = -16";
var A20 = "A: x = -3";
var Q21 = "A company charges a flat fee of 1500 dollars to rent a yacht. In addition, renters must pay 100 dollars per hour. Which equation shows the cost C to rent a yacht for h hours?";
var A21 = "C: C = 100h + 1500";
var Q22 = "What is the equation of a line that has a slope of 3/5 and a y-intercept of 2?";
var A22 = "A: 5y - 3x = 10";
var Q23 = "Make an equation for sixteen more than a number is 9.";
var A23 = "C: 16 + x = 9";
var Q24 = "There are b boys in the class. This is three more than four times the number of girls. How many girls are in the class?";
var A24 = "B: b = 3 + 4g";
var Q25 = "The sum of two numbers is 84, and one of them is 12 more than the other. What is the equation?";
var A25 = "D: x + 12 + x = 84";
var Q26 = "Anton's birthday party costs $2 for every guest he invites. Write an equation that shows the relationship between the guests x and the cost y.";
var A26 = "A: y = 2x";
var Q27 = "Allie jars 6 liters of jam every day. Write an equation that shows the relationship between the days x and the jam made y.";
var A27 = "C: y = 6x";
var Q28 = "Gabe can grow 8 flowers with every seed packet. Write an equation that shows the relationship between the number of seed packets x and the total number of flowers y.";
var A28 = "B: y = 8x";
var Q29 = "Bella's retirement party costs $6, plus an additional $1 for each guest she invites. Write an equation that shows the relationship between the guests x and the cost y.";
var A29 = "C: y = x + 6";
var Q30 = "Cole has already taken 4 pictures at home, and he expects to take 1 picture during every day of vacation. Write an equation that shows the relationship between the days of vacation x and the total number of pictures taken y.";
var A30 = "C: y = x + 4";
//Question Numbers
var N1 = Math.floor(Math.random() * 30) + 1;
var N2 = 0;
var N3 = 0;
var N4 = 0;
var N5 = 0;
var N6 = 0;
var N7 = 0;
var N8 = 0;
var N9 = 0;
var N10 = 0;
var N11 = 0;
var N12 = 0;
var N13 = 0;
var N14 = 0;
var N15 = 0;
var N16 = 0;
var N17 = 0;
var N18 = 0;
var N19 = 0;
var N20 = 0;
//On Game Load
function onLoad() {
do {
N2 = Math.floor(Math.random() * 30) + 1;
} while(N2 === N1);
do {
N3 = Math.floor(Math.random() * 30) + 1;
} while(N3 === N1 || N3 === N2);
do {
N4 = Math.floor(Math.random() * 30) + 1;
} while(N4 === N1 || N4 === N2 || N4 === N3);
do {
N5 = Math.floor(Math.random() * 30) + 1;
} while(N5 === N1 || N5 === N2 || N5 === N3 || N5 === N4);
do {
N6 = Math.floor(Math.random() * 30) + 1;
} while(N6 === N1 || N6 === N2 || N6 === N3 || N6 === N4 || N6 === N5);
do {
N7 = Math.floor(Math.random() * 30) + 1;
} while(N7 === N1 || N7 === N2 || N7 === N3 || N7 === N4 || N7 === N5 || N7 === N6);
do {
N8 = Math.floor(Math.random() * 30) + 1;
} while(N8 === N1 || N8 === N2 || N8 === N3 || N8 === N4 || N8 === N5 || N8 === N6 || N8 === N7);
do {
N9 = Math.floor(Math.random() * 30) + 1;
} while(N9 === N1 || N9 === N2 || N9 === N3 || N9 === N4 || N9 === N5 || N9 === N6 || N9 === N7 || N9 === N8);
do {
N10 = Math.floor(Math.random() * 30) + 1;
} while(N10 === N1 || N10 === N2 || N10 === N3 || N10 === N4 || N10 === N5 || N10 === N6 || N10 === N7 || N10 === N8 || N10 === N9);
do {
N11 = Math.floor(Math.random() * 30) + 1;
} while(N11 === N1 || N11 === N2 || N11 === N3 || N11 === N4 || N11 === N5 || N11 === N6 || N11 === N7 || N11 === N8 || N11 === N9 || N11 === N10);
do {
N12 = Math.floor(Math.random() * 30) + 1;
} while(N12 === N1 || N12 === N2 || N12 === N3 || N12 === N4 || N12 === N5 || N12 === N6 || N12 === N7 || N12 === N8 || N12 === N9 || N12 === N10 || N12 === N11);
do {
N13 = Math.floor(Math.random() * 30) + 1;
} while(N13 === N1 || N13 === N2 || N13 === N3 || N13 === N4 || N13 === N5 || N13 === N6 || N13 === N7 || N13 === N8 || N13 === N9 || N13 === N10 || N13 === N11 || N13 === N12);
do {
N14 = Math.floor(Math.random() * 30) + 1;
} while(N14 === N1 || N14 === N2 || N14 === N3 || N14 === N4 || N14 === N5 || N14 === N6 || N14 === N7 || N14 === N8 || N14 === N9 || N14 === N10 || N14 === N11 || N14 === N12 || N14 === N13);
do {
N15 = Math.floor(Math.random() * 30) + 1;
} while(N15 === N1 || N15 === N2 || N15 === N3 || N15 === N4 || N15 === N5 || N15 === N6 || N15 === N7 || N15 === N8 || N15 === N9 || N15 === N10 || N15 === N11 || N15 === N12 || N15 === N13 || N15 === N14);
do {
N16 = Math.floor(Math.random() * 30) + 1;
} while(N16 === N1 || N16 === N2 || N16 === N3 || N16 === N4 || N16 === N5 || N16 === N6 || N16 === N7 || N16 === N8 || N16 === N9 || N16 === N10 || N16 === N11 || N16 === N12 || N16 === N13 || N16 === N14 || N16 === N15);
do {
N17 = Math.floor(Math.random() * 30) + 1;
} while(N17 === N1 || N17 === N2 || N17 === N3 || N17 === N4 || N17 === N5 || N17 === N6 || N17 === N7 || N17 === N8 || N17 === N9 || N17 === N10 || N17 === N11 || N17 === N12 || N17 === N13 || N17 === N14 || N17 === N15 || N17 === N16);
do {
N18 = Math.floor(Math.random() * 30) + 1;
} while(N18 === N1 || N18 === N2 || N18 === N3 || N18 === N4 || N18 === N5 || N18 === N6 || N18 === N7 || N18 === N8 || N18 === N9 || N18 === N10 || N18 === N11 || N18 === N12 || N18 === N13 || N18 === N14 || N18 === N15 || N18 === N16 || N18 === N17);
do {
N19 = Math.floor(Math.random() * 30) + 1;
} while(N19 === N1 || N19 === N2 || N19 === N3 || N19 === N4 || N19 === N5 || N19 === N6 || N19 === N7 || N19 === N8 || N19 === N9 || N19 === N10 || N19 === N11 || N19 === N12 || N19 === N13 || N19 === N14 || N19 === N15 || N19 === N16 || N19 === N17 || N19 === N18);
do {
N20 = Math.floor(Math.random() * 30) + 1;
} while(N20 === N1 || N20 === N2 || N20 === N3 || N20 === N4 || N20 === N5 || N20 === N6 || N20 === N7 || N20 === N8 || N20 === N9 || N20 === N10 || N20 === N11 || N20 === N12 || N20 === N13 || N20 === N14 || N20 === N15 || N20 === N16 || N20 === N17 || N20 === N18 || N20 === N19);
questions();
}
function questions() {
var Q = "Q";
var x1 = Q.concat(N1);
//var x1 = Q + N1;
var y1 = document.getElementById("Q1");
y1.innerHTML = x1;
var z1 = document.getElementById("1").getElementsByClassName("answers");
z1.innerHTML = A1;
console.log(x1);
}
答案 0 :(得分:2)
您正在寻找的是一个&#34;数组&#34;。
var questions = [
{
question: "5 x 10<sup>7</sup> = ?",
answer: "Find a calculator."
},
// ...
];
然后:
questions.sort(function() {return Math.random()-0.5;});
// imperfect shuffle, you'll need more code for a good one but this works decently
和
console.log(questions[0].question);
console.log(questions[0].answer);
答案 1 :(得分:1)
x1的值是文字字符串,例如“Q2”。 javascript解释器永远不会将其视为变量,因为它是一个字符串。您需要重新构建应用程序,而不是使用这么多变量,构建一个对象。
var Q = {
'Q1': 'string',
'Q2': 'string',
}
等,然后您可以说Q[x1]
,您将获得相应属性的值。这是我能想到完成你所寻找的唯一方式。
答案 2 :(得分:0)
您可以在JavaScript中使用window对象。尝试用 console.log(window [x1])替换 console.log(x1); ;