I'm making a digital clock with ES5 ( no arrow stuff and so ). The location and temperature I'll do later. How can I improve/refactor the current code that I have with functional programming?
CodePen: https://codepen.io/Aurelian/pen/opaxqx?editors=1010
This is the HTML:
<div class="clock">
<div class="clock-ram">
<div class="clock-display">
<div class="clock-location">United Kingdom, Manchester</div>
<div class="clock-main">
<div class="clock-time"></div>
<div class="clock-widgets">
<div class="clock-widget clock-date"></div>
<div class="clock-widget clock-day"></div>
<div class="clock-widget clock-temperature">NAN</div>
</div>
</div><!-- /clock-main -->
<ul class="clock-weekdays">
</ul>
</div><!-- /display -->
</div><!-- /ram -->
</div> <!-- /clock -->
This is the JS:
// Get te location
// Make the : blink
//var location = document.querySelector('.clock-location');
function abbrev(a,b) {
return a.substr(0,b);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i
};
return i;
}
function displayTime() {
var clockTime = document.querySelector('.clock-time');
var clockDate = document.querySelector('.clock-date');
var clockDay = document.querySelector('.clock-day');
var clockWeekdays = document.querySelector('.clock-weekdays');
while(clockWeekdays.firstChild) clockWeekdays.removeChild(clockWeekdays.firstChild);
var date = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var singleDay = [];
for (var i = 0; i < weekday.length; i++) {
var day = weekday[i];
var li = document.createElement('li');
li.textContent = abbrev(day,3);
if (i === date.getDay()) {
li.classList.add('active');
}
clockWeekdays.appendChild(li);
}
// Mechanics
var todayDay = weekday[date.getDay()];
var h = date.getHours();
var m = checkTime(date.getMinutes());
var s = checkTime(date.getSeconds());
var year = date.getFullYear();
var month = date.getMonth();
// Display
clockDay.textContent = abbrev(todayDay,3);
clockTime.textContent = h + ":" + m + ":" + s;
clockDate.textContent = monthNames[month] + " / " + year;
}
window.setInterval(displayTime, 1000);
//displayTime();
// Get the time
//Loop Throw weekdays - show 3 letters
CodePen: https://codepen.io/Aurelian/pen/opaxqx?editors=1010