jeudi 6 mai 2021

Choose and delete random word from array in Javascript

I'm trying to remake a wordgame I made in Python in Javascript. I'm having trouble to do some basic things.

The script should load a list of words and when you click the button, it should show a random word from the list. I tried several ways and I did not come further than the index number showing up and not refreshing when the button was clicked again.

So my question at this stage would be: how do I choose a random array item with clicking, and removing it after so it doesn't appear a second time when you click again. I do want to add a time limit of showing the words and a TTS afterwards, but that will come later.

<html>
<head>
    <title>Aliyah's dictee spel</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="header">
    <h1>Hej! Velkommen til Aliyahs diktee!</h1>
    </div>
    <div id="Random_word">
        <h2 id="Empty">Click new word to start</h2>
        <button id="refresh_word">new word</button>

    </div>
    <input type="text" id="input1" />
    <input type="submit" name="ok" value="ok"/>
    <script>
        
    var words = ["test1", "test2", "test3"];
    var wordsCorrect = 0;
    var wordsWrong = 0;

    newWord(words)
    {
        var random = Math.floor(Math.random() * words.length);
        document.getElementById("Empty").innerHTML = random;
    };
    document.getElementById("refresh_word").addEventListener("click", newWord());

    

    </script>
</body>
</html>



Aucun commentaire:

Enregistrer un commentaire