0
I’m wandering about on a way to make sure that as all elements of the array in question, go all over again.
See this my source code below:
Code
<html>
<body>
<button onclick="next()">APAGAR</button>
</body>
<script>
var total = 4,
letras = ["A", "B", "C", "D"];
function next() {
// Antes de começar a apagar o primeiro indice, conferir se array
// está totalmente vazio e popular array novamente caso positivo
if (total.length === 0) {
// Para popular a array use
total = letras.slice(0);
}
// A cada clique, eliminar o elemento [0]
var indice = letras.splice(0, 1);
alert(indice, letras);
}
</script>
</html>
But when it comes to the end, it does not return what I need. It would be to go back all over again as from the beginning. Like a roulette wheel.
I don’t quite understand, you want to remove the first element of the array every time you click the button and return the array without that element?
– Gabriel C.
@Gabrielc. He is already removing. Now I need him to detect that the
arraybeing empty to re-populate it with elements of itself. That’s why I put in the title "clone".– Diego Henrique
then why are you initiating the variable
totalwith the value4, and then he’s checking out the propertylength? That way the parole won’t betruesincetotal.lengthwill returnundefined– Gabriel C.
@Gabrielc. On another occasion had not defined this variable
totalthis way. In another test had so assignedvar total, link = [ .. ], but in the console it said that the variable was not definedtotal. So I decided to make this change, to see if I could.– Diego Henrique
@Gabrielc. I know I’m on the right track, because the method
slicereturns a copy of aarrayin a new objectarray. I just can’t seem to finish this step.– Diego Henrique