Today, I delved into the world of JavaScript and decided to test my skills by creating a page that changes images when clicking on a div. Everything worked perfectly until I wanted to add an input element to specify how many steps to jump each time the button is clicked. While trying to implement this feature, I noticed that the variable seemed to be accumulating values when I checked it through console.log. The first code snippet works flawlessly with fixed values, but the second one fails to function as expected. The aim was for the second code to dynamically change the colors based on the user-defined step size. However, this did not pan out as planned.
Check out the functioning code here
window.onload = function(){
var x = 0;
document.getElementById("teste").onclick = function(){myFunction()};
function myFunction(){
if (x == 0){
document.getElementById("teste").style.backgroundColor = "red";
document.getElementById("taiga").style.opacity = 1;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += document.getElementById("number").value;
} else if (x == 1){
document.getElementById("teste").style.backgroundColor = "blue";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 1;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += document.getElementById("number").value;
} else if (x == 2){
document.getElementById("teste").style.backgroundColor = "yellow";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 1;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += document.getElementById("number").value;
} else if (x == 3){
document.getElementById("teste").style.backgroundColor = "purple";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 1;
x += document.getElementById("number").value;
} else if (x == 4){
document.getElementById("teste").style.backgroundColor = "black";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 1;
document.getElementById("moge-ko").style.opacity = 0;
x = 0;
}
console.log(x);
};
};
Check out the non-functioning code here
window.onload = function(){
var x = 0;
document.getElementById("teste").onclick = function(){myFunction()};
function myFunction(){
if (x == 0){
document.getElementById("teste").style.backgroundColor = "red";
document.getElementById("taiga").style.opacity = 1;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += 1;
} else if (x == 1){
document.getElementById("teste").style.backgroundColor = "blue";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 1;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += 1;
} else if (x == 2){
document.getElementById("teste").style.backgroundColor = "yellow";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 1;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 0;
x += 1;
} else if (x == 3){
document.getElementById("teste").style.backgroundColor = "purple";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 0;
document.getElementById("moge-ko").style.opacity = 1;
x += 1;
} else if (x >= 4){
document.getElementById("teste").style.backgroundColor = "black";
document.getElementById("taiga").style.opacity = 0;
document.getElementById("nanachi").style.opacity = 0;
document.getElementById("azami").style.opacity = 0;
document.getElementById("kcalb").style.opacity = 1;
document.getElementById("moge-ko").style.opacity = 0;
x = 0;
}
console.log(x);
};
};
How can I modify the second piece of code to make it work as intended?