gpt4 book ai didi

javascript 随机生成 nr + 循环

转载 作者:行者123 更新时间:2023-11-28 03:48:26 24 4
gpt4 key购买 nike

我正在尝试制作一个程序,它从 X 个问题中随机挑选 10 个问题(目前随机编写了 14 个问题),而不会再次挑选相同的问题,但我已经在这个垃圾问题上被困了 4 个小时。

我的问题是“randomNumbers()”函数。该函数生成随机数(分别标记问题),然后检查 numberArray 是否已经生成了数字。如果没有,则该数字应该被推送到数组中。

我想我发现问题是 ## VERSION 1 ## for 循环 if/else 条件。有什么帮助吗? :(

//编辑, while (true) 是处理这个问题的正确方法吗?

//不介意##VERSION 2## 这是我尝试解决问题的第一种方法。

(通过大量控制台日志来确定错误:P)

HTML

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<title>Testimiskeskkond</title>
<!--<link rel="stylesheet" type="text/css" href="style.css"/> -->
<script src="scripts/jquery-2.1.4.min.js"></script>
<script src="scripts/js.js"></script>
</head>
<body>
<button onclick="startQuiz()">Alusta testimist</button>
<div id="question1"></div>
<div id=questions></div>
</body>

JAVASCRIPT

var amountOfQuestions = 11;
var questionCounter = 0;
var numberArray = new Array();


var questions = new Array();
questions[0] = 'Mitu kassi elab P2rnus?',
questions[1] = 'Mis on kassi nimi?',
questions[2] = 'Mida kass teeb?',
questions[3] = 'Millal kass syndis?',
questions[4] = 'Mitu hammast kassil on?',
questions[5] = 'Mitu kyynt on kassil?',
questions[6] = 'Mitu k6rva on kassil?',
questions[7] = 'Mis v2rvi on kass?',
questions[8] = 'Tooli v2rvus?',
questions[9] = 'Laua suurus?',
questions[10] = 'Lemmik jook?',
questions[11] = 'Lemmik s88k?',
questions[12] = 'Raamatupoe nimi?',
questions[13] = 'Viinapoe nimi?';


function startQuiz() {
var setQuestions = "";

while (questionCounter < amountOfQuestions) {
var random = randomNumbers();
console.log(random + "appppppi");

if (questionCounter < amountOfQuestions) {
setQuestions += questions[random] + "<br>";
questionCounter += 1;
} else {
setQuestions += questions[random];
questionCounter += 1;
}

}

$('#questions').html(setQuestions);

}

function randomNumbers() {
var arrLength = numberArray.length;

while (true) {
console.log(arrLength);

var randomNr = Math.floor(Math.random() * 14);
console.log(randomNr + " tereeeeeeeeee");

/*
######################
########### VERSION 1
######################*/

if (arrLength == 0) {
console.log("pppppppppppp");
numberArray.push(randomNr);
break;
} else if (arrLength == 1) {
console.log("oooooooooooo");
if (numberArray[0] == randomNr) {
console.log("xxxxxxxxxxxxx");
break;
} else {
console.log("rrrrrrrrrrrrrrr");
numberArray.push(randomNr);
break;
}
} else {
for (var i = 0; i < arrLength-1; i++) {
console.log("yyyyyyyyyyyyyyyyyyyy");
if (numberArray[i] == randomNr) {
console.log("qqqqqqqqqqqqqqqqqqqq");
continue;
} else {
console.log("zzzzzzzzzzzzzz")
numberArray.push(randomNr);
break;
}
}
}


/*
######################
########### VERSION 2
######################

if (arrLength > 0) {
console.log("oooooooooooo");
for (var i = 0; i < arrLength; i++) {
console.log("yyyyyyyyyyyyyyyyyyyy");
if (numberArray[i] == randomNr) {
console.log("qqqqqqqqqqqqqqqqqqqq");
continue;
} else {
console.log("zzzzzzzzzzzzzz")
numberArray.push(randomNr);
break;
}
}
} else {
console.log("pppppppppppp");
numberArray.push(randomNr);
break;

} */
}


return randomNr;
}

最佳答案

let selectionCount = 10;
let randomQuestions = [];

while(randomQuestions.length < selectionCount) {
let randomNr = Math.floor(Math.random() * questionList.length);

if(!randomQuestions.includes(randomNr)) {
randomQuestions.push(randomNbr);
}
}

关于javascript 随机生成 nr + 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48245820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com