I find the following problem with the SimonSays :
I want to make the effect of coloring the box of a highlighted color that you have to press, and the second one to turn to a more subdued color. Through changing id
with setAttribute
. I get to highlight it (change id
to highlighted), but when trying to return it to the% original% co, it ignores me completely.
window.onload = function() {
boton = document.querySelector('input[name=enviar]');
boton.addEventListener('click', function () {
var orden = 0;
var posiciones = generaPosicion();
resaltarColores(posiciones);
})
}
function generaPosicion(valor) {
var numColores = valor || 4;
var arrPos = new Array();
var numeroFijoColores = 3;
for(var i=0, fin = numColores; i<fin;i++){
arrPos[i] = Math.round(Math.random() * numeroFijoColores );
}
return arrPos;
}
function resaltarColores(posiciones) {
console.log(posiciones)
var idColoresA = Array('rojoDes', 'azulDes', 'amarilloDes', 'verdeDes');
var botones = document.getElementsByTagName('button');
var x = 0, fin = posiciones.length;
var auxPos;
var idDes = setInterval(function() {
devolverColorInicial(posiciones[x]);
botones[posiciones[x]].setAttribute('id', idColoresA[posiciones[x]]);
x++;
if(!botones[posiciones[x]]){
clearInterval(idDes);
auxPos = x--;
if(auxPos!=posiciones.length)
devolverColorInicial(posiciones[auxPos]);
}
}, 1000);
}
function devolverColorInicial(posicionCosas) {
var idColoresB = Array('rojo', 'azul', 'amarillo', 'verde');
var botones = document.getElementsByTagName('button');
console.log(botones[posicionCosas]);
console.log(idColoresB[posicionCosas]);
botones[posicionCosas].setAttribute('id', idColoresB[posicionCosas]);
console.log(botones[posicionCosas]);
}
button {
height: 100px;
width: 100px;
}
#col1, #col2, #col3 {
display: flex;
justify-content: center;
}
#col3{
margin-top: 5%;
}
#rojo{
background-color:rgb(231, 112, 112) ;
}
#azul{
background-color: rgb(127, 113, 233);
}
#amarillo{
background-color: rgb(227, 214, 129);
}
#verde{
background-color: rgb(152, 231, 97);
}
#rojoDes{
background-color:red;
}
#azulDes{
background-color: blue;
}
#amarilloDes{
background-color:yellow;
}
#verdeDes{
background-color: green;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style/main.css">
<script src="script/main.js"></script>
<title>SimonSays</title>
</head>
<body>
<div id="col1">
<button id = "rojo"></button>
<button id = "azul"></button>
</div>
<div id="col2">
<button id = "amarillo"></button>
<button id = "verde"></button>
</div>
<div id="col3"> <input type="button" name="enviar" value="Inicio"></div>
</body>
</html>