Last active
February 27, 2018 14:15
-
-
Save trafficinc/77388cb355589b9fd2a43cd5332b9e6c to your computer and use it in GitHub Desktop.
Alternative to switch statement, JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
== requires jquery ==== | |
==== html ====== | |
<div id="show"></div> | |
===== js ====== | |
function switchcase(cases,defaultCase,key) { | |
if (cases.hasOwnProperty(key)) { | |
return cases[key]; | |
} else { | |
return defaultCase; | |
} | |
} | |
var getDay = switchcase({ | |
0: 'Sunday', | |
1: 'Monday', | |
2: 'Tuesday', | |
3: 'Wednesday', | |
4: 'Thursday', | |
5: 'Friday', | |
6: 'Saturday' | |
},'Sunday', new Date().getDay()); | |
$('#show').html(getDay); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment