gpt4 book ai didi

javascript - 如果已经选择了值,如何让 JS 选择不同的值?

转载 作者:行者123 更新时间:2023-12-01 08:31:28 25 4
gpt4 key购买 nike

我有一个问题,我无法让 JS 始终打印出 3 个不同的值,它有时会选择重复的值。有人可以帮忙吗?

var receivedArray = JSON.parse('<?php echo json_encode($unserialize);?>');
const random = receivedArray;
const correctAnswer = random[Math.floor(Math.random() * random.length)];
const guess1 = random[Math.floor(Math.random() * random.length)];
const guess2 = random[Math.floor(Math.random() * random.length)];


$(document).ready(function() {
$("#test-div").append(
"<div class=\"row\">\n" +
"<div class=\"col-6\">\n" +
"<img id=\"testImage\" src=\"\" alt='...' height=\"540px\"/>\n" +
"</div>\n" +
"<div class=\"col-6\">\n" +
"<h4 class=\"Guess\" id=\"Guess\">ATMINI JŪRNIEKU MEZGLA NOSAUKUMU</h4>\n" +
"<div id=\"shuffle\">" +
"<div class=\"btn guesses\" >" + correctAnswer.nameLV + "</div><br>" +
"<div class=\"btn guesses\" >" + guess1.nameLV + "</div><br>" +
"<div class=\"btn guesses\" >" + guess2.nameLV + "</div>" +
"</div>\n" +
"</div>\n" +
"</div>\n"

);
$("#testImage").attr("src", "../Images/uploads/" + correctAnswer.Image);
console.log("BS")
});

最佳答案

您应该使用splice()每当选择答案时,从random数组中删除项目:

const correctAnswer = random.splice(Math.floor(Math.random() * random.length), 1);
const guess1 = random.splice(Math.floor(Math.random() * random.length), 1);
const guess2 = random.splice(Math.floor(Math.random() * random.length), 1);

关于javascript - 如果已经选择了值,如何让 JS 选择不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60506963/

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