My objective is to have the following code run automatically without the need for user input. It should also execute in a loop instead of running through the sequence only once. I attempted to modify the code, but it either failed to work or altered the shape of the traffic light. Appreciate any assistance.
<!DOCTYPE html>
<html>
<head>
<style>
</style>
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: sans-serif;
}
#controlPanel {
float: left;
padding-top: 30px;
}
.button {
background-color: gray;
color: white;
border-radius: 10px;
padding: 20px;
text-align: center;
margin: 90px 40px;
cursor: pointer;
}
#traffic-light {
width: 150px;
float: left;
background-color: #333;
border-radius: 40px;
margin: 30px 0;
padding: 20px;
}
.lightbulb {
height: 100px;
width: 100px;
background-color: #111;
border-radius: 50%;
margin: 25px auto;
transition: background 500ms;
}
#controlPanel>h1 {
margin-top: 30px;
margin-bottom: 30px;
}
</style>
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body>
<div id="controlPanel">
<h1 id="autoLights" class="button">Auto</h1>
</div>
<div id="traffic-light">
<div id="stopLight" class="lightbulb"></div>
<div id="slowLight" class="lightbulb"></div>
<div id="goLight" class="lightbulb"></div>
</div>
<script type="text/javascript">
document.getElementById('autoLights').onclick = autoLights;
function stopRed() {
Lights();
document.getElementById('stopLight').style.backgroundColor = "red";
}
function slowYellow() {
Lights();
document.getElementById('slowLight').style.backgroundColor = "orange";
}
function goGreen() {
Lights();
document.getElementById('goLight').style.backgroundColor = "green";
}
function Lights() {
document.getElementById('stopLight').style.backgroundColor = "black";
document.getElementById('slowLight').style.backgroundColor = "black";
document.getElementById('goLight').style.backgroundColor = "black";
}
function lightOne(num) {
Lights();
switch (num) {
case 1:
stopRed();
break;
case 2:
slowYellow();
break;
case 3:
goGreen();
break;
default:
alert("you made some error");
}
}
counter = 0;
maxSec = 3;
function timer() {
setTimeout(function() {
counter++;
lightOne(counter);
if (counter == maxSec) {
return;
}
timer();
}, 2000);
}
function autoLights() {
counter = 1;
lightOne(counter);
timer();
}
</script>
</body>
</html>
<script type="text/javascript">
</script>
</body>
</html>
Desired code structure was to resemble the sequence with a new button functionality. I faced challenges replicating the initial code's shape.
<!DOCTYPE html>
<html>
<head>
<style>
.traffic-light {
height: 40px;
width: 40px;
margin: 1px;
background-color: black;
border-radius: 20px;
border: solid 1px #000
}
.surround {
padding: 10px;
background-color: grey;
display: inline-block;
}
</style>
<script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<body>
<div id="wrapper">
<div class="surround">
<div id="red-light" class="traffic-light"></div>
<div id="yellow-light" class="traffic-light"></div>
<div id="green-light" class="traffic-light"></div>
</div>
</div>
</body>
</html>
<script type="text/javascript">
function trafficLights() {
var sequenceData = [
[5, 1, 0, 0],
[2, 1, 1, 0],
[5, 0, 0, 1],
[3, 0, 1, 0]
],
lights = [],
index = 0;
for (var i = 0, elemId;
(elemId = arguments[i]); i++)
lights[i] = document.getElementById(elemId);
function display() {
if (index >= sequenceData.length)
index = 0;
for (var i = 0, cv, dLen = lights.length; i < dLen; i++)
lights[i].style.backgroundColor = (sequenceData[index][i + 1] ? lights[i].id.match(/^[a-z]+/i).toString() : '#000');
setTimeout(display, sequenceData[index++][0] * 977);
}
display();
}
window.onload = function() {
trafficLights("red-light", "yellow-light", "green-light");
};
</script>
</body>
</html>