jeudi 1 octobre 2020

JS get random value from array and update array

I need your help on this! I'm generating an array which corresponds to a question number.

var arrayCharge = [];
for (var i = 2; i <= 45; i++) {
arrayCharge.push(i);
}

then I use this number to append the corresponding question, answer then click. Then I'm getting a new value from the array like this

const randomQ = arrayCharge;
const random = Math.floor(Math.random() * randomQ.length);

It works and a new question is charged but the array is still the same. I've tried this

var remQ = arrayCharge.indexOf(randomQ[random]);
arrayCharge.splice(remQ,1);

But It doesn't work ;-(

Thanks a lot for your help. Nicolas

Here is the entire code to help comprehension! sorry for that, I should have done it from the begining.

<!DOCTYPE HTML>
<!--
    Hyperspace by HTML5 UP
    html5up.net | @ajlkn
    Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>

<head>
    <title>Repérez vos messages contraignants - Quiz</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="assets/css/main.css" />
    <noscript>
        <link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
</head>

<body class="is-preload">
    <!-- Sidebar -->
    <!-- <section id="sidebar">
    </section> -->
    <!-- Wrapper -->
    <div id="wrapper">
        <!-- Intro -->
        <section id="intro" class="wrapper style1 fullscreen fade-up">
            <div class="inner">
                <header>
                    <button id="start">Commencer</button>
                    <p>&nbsp;</p>
                </header>
                <form action="" method="post">
                    <p id="Qnum"></p>
                    <p id="Q" data-qnumber="" data-type=""></p>
                    <section id="answer">
                        <input type="submit" id="1" name="R1" value="Non">
                        <input type="submit" id="2" name="R2" value="Parfois">
                        <input type="submit" id="3" name="R3" value="Souvent">
                        <input type="submit" id="4" name="R4" value="Oui">
                    </section>
                </form>
            </div>
        </section>
        <!-- Footer -->
        <!-- Scripts -->
        <script src="assets/js/jquery.min.js"></script>
        <script src="assets/js/jquery.scrollex.min.js"></script>
        <script src="assets/js/jquery.scrolly.min.js"></script>
        <script src="assets/js/browser.min.js"></script>
        <script src="assets/js/breakpoints.min.js"></script>
        <script src="assets/js/util.js"></script>
        <script src="assets/js/main.js"></script>
        <script>
        $(document).ready(function() {

            if (localStorage.getItem("clic") >= 45) {
                console.log('45');
                sessionStorage.clear();
                localStorage.clear();
            }

            var Q1 = [1, "My first question", "FP"];
            var Q2 = [2, "My second question", "SP"];
            var Q3 = [3, "My third question", "SE"];
            var Q4 = [4, "My foutrh question", "DP"];
            var Q5 = [5, "My fifth question", "FP"];    
            //etc... until Q45


            if (sessionStorage.getItem("FP") == null) {

                $("form").attr("action", "driversV2.php");
                $("#answer").hide();
                $("#start").click(function() {

                    $("#Qnum").append(1+" / 45");
                    $("#Q").append(Q1[1]).attr("data-qnumber", Q1[0]).attr("data-type", Q1[2]);
                    $("#answer").show();
                    $("header").hide();

                    var pageType = $("#Q").attr("data-type");
                    $("input").click(function() {
                        var reponse = this.id;
                        sessionStorage.setItem(pageType, reponse);
                        localStorage.setItem("clic", 1);
                    });
                });

            } else {
                $("header").hide();
                var clicNum = parseInt(localStorage.getItem("clic"));
                var QNumber = clicNum + 1;
                var arrayCharge = [];
                for (var i = 2; i <= 45; i++) {
                  arrayCharge.push(i);
                }
                const randomQ = arrayChargeNew;
                const random = Math.floor(Math.random() * randomQ.length);
                console.log('valeur random new = '+randomQ[random]);
                var QCharge = "Q" + randomQ[random];
                var Charge = eval(QCharge);
                localStorage.setItem("random",randomQ[random]);
   
                $("#Qnum").append(QNumber+" / 45");
                $("#Q").append(Charge[1]).attr("data-qnumber", Charge[0]).attr("data-type", Charge[2]);
                //création de la variable du type de question
                var pageType = $("#Q").attr("data-type");
                //alert(sessionStorage.getItem(pageType));
                if (localStorage.getItem("clic") < 44) {

                    $("form").attr("action", "driversV2.php");

                    if (sessionStorage.getItem(pageType) != null) {
                        var x = parseInt(sessionStorage.getItem(pageType));
                        $("input").click(function() {
                            var reponse = parseInt(this.id);
                            var addition = reponse + x;
                            sessionStorage.setItem(pageType, addition);
                            var clic = parseInt(localStorage.getItem("clic"));
                            localStorage.setItem("clic", clic + 1);
                        });
                    } else {
                        $("input").click(function() {
                            var reponse = this.id;
                            sessionStorage.setItem(pageType, reponse);
                            var clic = parseInt(localStorage.getItem("clic"));
                            localStorage.setItem("clic", clic + 1);

                        });
                    }
                } else {
                    $("form").attr("action", "driversResultat.php");

                    if (sessionStorage.getItem(pageType) != null) {
                        var x = parseInt(sessionStorage.getItem(pageType));
                        $("input").click(function() {
                            var reponse = parseInt(this.id);
                            var addition = reponse + x;
                            sessionStorage.setItem(pageType, addition);
                            var clic = parseInt(localStorage.getItem("clic"));
                            localStorage.setItem("clic", clic + 1);

                        });
                    } else {
                        $("input").click(function() {
                            var reponse = this.id;
                            sessionStorage.setItem(pageType, reponse);
                            var clic = parseInt(localStorage.getItem("clic"));
                            localStorage.setItem("clic", clic + 1);

                        });
                    }
                }
            }
        });
        </script>
</body>

</html>



Aucun commentaire:

Enregistrer un commentaire