JSFiddle - React, Tailwind, and code Playground

by zxzc0

HTML

<div class="word" id="word1">-</div>
<div class="bt" id="bt">random</div>

CSS

* {
  box-sizing: border-box;
  -moz-user-select: none;
  user-select: none;
}

body {
  background-color: #20262e;
  color: white;
  font-size: 20px;
}

.word {
  padding: 10px;
}

.bt {
  width: 80px;
  height: 50px;
  border: 1px solid white;
  display: inline-block;
  color: white;
  font-size: 20px;
  padding-top: 12px;
  text-align: center;
  cursor: pointer;
}

JavaScript

//arrays
let words = [{
    q: 'A',
    a: 'a'
  },
  {
    q: 'B',
    a: 'b'
  },
   {
    q: 'C',
    a: 'c'
  },
/*  {
    q: 'D',
    a: 'd'
  },
  {
    q: 'E',
    a: 'e'
  },
  {
    q: 'F',
    a: 'f'
  }, */
];

let currentIndex;
let randIndex;
const word1 = document.querySelector('#word1');
const bt = document.querySelector('#bt');

function witam() {
  while (randIndex === currentIndex) {
    randIndex = Math.floor(Math.random() * words.length);
  }
}

function randWord() {
  witam();
  currentIndex = randIndex;
  word1.innerText = words[currentIndex].q;
}

bt.addEventListener("click", function() {
  randWord();
});

randWord();