-3
Good evening! I’m new to Javascript. I did a study of the word memorization type where I pick a certain number of words within an Array. The words are random and shown one by one. But I would like to implement a button to show all the words that were chosen in a list, as an answer to check later. Can someone give me a way? Thank you.
o index:
<div id="container">
<h1>Random</h1>
<div id="random"></div>
<select id="palavras">
<option value="10" selected>10 palavras</option>
<option value="20" >20 palavras</option>
<option value="30">30 palavras</option>
</select>
<button id="iniciar" >Iniciar</button>
<button id="resposta" style=" display: none;">Resposta</button>
<select id="tempo">
<option value="1000" selected>1 segundo</option>
<option value="2000" >2 segundos</option>
<option value="5000">5 segundos</option>
</select>
<div id="lista" style=" display: none; margin-top: 50px;">
</div>
O script.js
let random = document.getElementById('random');
let resposta = document.getElementById('resposta');
let tempo = document.getElementById('tempo');
let palavras = document.getElementById('palavras');
let inicar= document.getElementById('iniciar');
function randomWords(min, max){
let step1 = max - min +1;
let step2 = Math.random() * step1;
let result = Math.floor(step2) + min;
return result;
}
let words = [
'Barriga',
'Bicicleta',
'Formiga',
'Garrafa',
'Teclado',
'Mouse',
'Caneca',
'Cerveja',
'Barata',
'Cigarro',
'Café',
'Copo',
'Poste',
'Bola',
'Balão',
'Igreja',
'Padre',
'Carro',
'Caminhão',
'Monitor',
];
function verificaDez(){
if(words.length == 20){
random.innerText = "Clique em resposta par ver o resultado";
document.getElementById('resposta').style.display = "block";
return;
}
let randomIndex = randomWords (0,words.length-1) ;
let randomWord = words[randomIndex];
words.splice(randomIndex,1);
random.innerText = randomWord;
}
function verificaVinte(){
if(words.length == 10){
random.innerText = "Clique no botão resposta para ver o resultado";
document.getElementById('resposta').style.display = "block";
return;
}
let randomIndex = randomWords (0,words.length-1) ;
let randomWord = words[randomIndex];
words.splice(randomIndex,1);
random.innerText = randomWord;
}
function verificaTrinta(){
if(words.length == 0){
random.innerText = "Clique em resposta par ver o resultado";
document.getElementById('resposta').style.display = "block";
return;
}
let randomIndex = randomWords (0,words.length-1) ;
let randomWord = words[randomIndex];
words.splice(randomIndex,1);
random.innerText = randomWord;
}
iniciar.addEventListener("click", function( ) {
if(palavras.value == 10){
setInterval(verificaDez, tempo.value);
}else if ( palavras.value == 20){
setInterval(verificaVinte, tempo.value);
}else if( palavras.value == 30){
setInterval(verificaTrinta, tempo.value);
}
}
);
resposta.addEventListener("click", function( ) {
}
);
https://jsfiddle.net/wnobre/epzLm05r/ " Example of how it turned out"
This answers your question? Generate multiple random numbers without repetition
– Rafael Tavares
@Rafaeltavares I’ve actually managed to generate the random words! What I didn’t get was after showing the random words , recover and show all at once in a list. I don’t know if I could understand, rs
– wnobre
Do you say in the answer? Your event reply click the function is empty...
– MagicHat
@Magichat Yes! It’s empty, because I don’t know how to return those words that were shown randomly. That’s exactly my question.
– wnobre
Um, I get it... and you didn’t even try anything?
– MagicHat
@Magichat I tried everything I knew, rs! But, I am beginner and nothing worked.
– wnobre
So the ideal is to put the code you tried...
– MagicHat