Skip to content

Instantly share code, notes, and snippets.

@JNaeemGitonga
Last active March 30, 2017 22:53
Show Gist options
  • Save JNaeemGitonga/46d093ba03e1c1f311ccc52e871f3d10 to your computer and use it in GitHub Desktop.
Save JNaeemGitonga/46d093ba03e1c1f311ccc52e871f3d10 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/mawoceg
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
* {
box-sizing: border-box;
}
main {
padding: 50px 20px;
}
.traffic-light-box {
width: 100px;
border: 4px solid orange;
border-radius: 15%;
padding: 7px;
margin: 0 auto;
margin-bottom: 20px;
}
.traffic-light {
margin: 0 auto;
height: 80px;
width: 80px;
border: 1px black solid;
border-radius: 50%;
margin-bottom: 5px;
}
.red-light {
border-color: red;
}
.yellow-light {
border-color: yellow;
}
.green-light {
border-color: green;
}
.red-on {
background-color: red;
}
.yellow-on {
background-color: yellow;
}
.green-on {
background-color: green;
}
.controls {
text-align: center;
}
</style>
</head>
<body>
<main>
<div class="traffic-light-box">
<div class="traffic-light red-light"></div>
<div class="traffic-light yellow-light"></div>
<div class="traffic-light green-light"></div>
</div>
<div class="controls">
<button class="js-control-lights">click me</button>
</div>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</main>
<script id="jsbin-javascript">
function doTrafficLights() {
var activeLight = getActiveLight();
if (activeLight === 'green')
turnGreen();
if (activeLight === 'yellow')
turnYellow();
if (activeLight === 'red')
turnRed();
console.log(activeLight);
}
/* I did not know that you could have
three 'if' statements in the as part of the same
function. I thought it had to be
if/else if/else. Syntax seem to be a
hurdle for me too. and if I remove console.log(activeLight);
it still works. Why?
*/
function turnOffLights() {
$('.traffic-light').removeClass('yellow-on red-on green-on');
}
function turnGreen() {
turnOffLights();
$('.green-light').addClass('green-on');
}
function turnYellow() {
turnOffLights();
$('.yellow-light').addClass('yellow-on');
}
function turnRed() {
turnOffLights();
$('.red-light').addClass('red-on');
}
function getActiveLight() {
return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
}
function handleClicks() {
$('.js-control-lights').click(function() {
doTrafficLights();
});
}
$(handleClicks);
</script>
<script id="jsbin-source-css" type="text/css">* {
box-sizing: border-box;
}
main {
padding: 50px 20px;
}
.traffic-light-box {
width: 100px;
border: 4px solid orange;
border-radius: 15%;
padding: 7px;
margin: 0 auto;
margin-bottom: 20px;
}
.traffic-light {
margin: 0 auto;
height: 80px;
width: 80px;
border: 1px black solid;
border-radius: 50%;
margin-bottom: 5px;
}
.red-light {
border-color: red;
}
.yellow-light {
border-color: yellow;
}
.green-light {
border-color: green;
}
.red-on {
background-color: red;
}
.yellow-on {
background-color: yellow;
}
.green-on {
background-color: green;
}
.controls {
text-align: center;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">function doTrafficLights() {
var activeLight = getActiveLight();
if (activeLight === 'green')
turnGreen();
if (activeLight === 'yellow')
turnYellow();
if (activeLight === 'red')
turnRed();
console.log(activeLight);
}
/* I did not know that you could have
three 'if' statements in the as part of the same
function. I thought it had to be
if/else if/else. Syntax seem to be a
hurdle for me too. and if I remove console.log(activeLight);
it still works. Why?
*/
function turnOffLights() {
$('.traffic-light').removeClass('yellow-on red-on green-on');
}
function turnGreen() {
turnOffLights();
$('.green-light').addClass('green-on');
}
function turnYellow() {
turnOffLights();
$('.yellow-light').addClass('yellow-on');
}
function turnRed() {
turnOffLights();
$('.red-light').addClass('red-on');
}
function getActiveLight() {
return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
}
function handleClicks() {
$('.js-control-lights').click(function() {
doTrafficLights();
});
}
$(handleClicks);
</script></body>
</html>
* {
box-sizing: border-box;
}
main {
padding: 50px 20px;
}
.traffic-light-box {
width: 100px;
border: 4px solid orange;
border-radius: 15%;
padding: 7px;
margin: 0 auto;
margin-bottom: 20px;
}
.traffic-light {
margin: 0 auto;
height: 80px;
width: 80px;
border: 1px black solid;
border-radius: 50%;
margin-bottom: 5px;
}
.red-light {
border-color: red;
}
.yellow-light {
border-color: yellow;
}
.green-light {
border-color: green;
}
.red-on {
background-color: red;
}
.yellow-on {
background-color: yellow;
}
.green-on {
background-color: green;
}
.controls {
text-align: center;
}
function doTrafficLights() {
var activeLight = getActiveLight();
if (activeLight === 'green')
turnGreen();
if (activeLight === 'yellow')
turnYellow();
if (activeLight === 'red')
turnRed();
console.log(activeLight);
}
/* I did not know that you could have
three 'if' statements in the as part of the same
function. I thought it had to be
if/else if/else. Syntax seem to be a
hurdle for me too. and if I remove console.log(activeLight);
it still works. Why?
*/
function turnOffLights() {
$('.traffic-light').removeClass('yellow-on red-on green-on');
}
function turnGreen() {
turnOffLights();
$('.green-light').addClass('green-on');
}
function turnYellow() {
turnOffLights();
$('.yellow-light').addClass('yellow-on');
}
function turnRed() {
turnOffLights();
$('.red-light').addClass('red-on');
}
function getActiveLight() {
return (['red', 'green', 'yellow'])[Math.floor(Math.random() * 3)];
}
function handleClicks() {
$('.js-control-lights').click(function() {
doTrafficLights();
});
}
$(handleClicks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment