Created
March 5, 2018 02:49
-
-
Save dodysw/cbd33f279302ea519c0f6da11039b0b7 to your computer and use it in GitHub Desktop.
Found something at https://www.urbanhire.com/careers#full-stack-engineer-urbanhire-1ljj, my attempt to answer
This file contains 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
var zeroPad = function(n) { | |
return n > 9 ? '' + n : '0' + n; | |
} | |
var formatDate = function(dt) { | |
return "" + dt.getFullYear() + "-" + zeroPad(dt.getMonth() + 1) + "-" + zeroPad(dt.getDate()); | |
} | |
exports.genDateRange = function(_startDate, _endDate) { | |
var startDate = new Date(_startDate); | |
var endDate = new Date(_endDate); | |
var dateRange = []; | |
if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) { | |
return dateRange; | |
} | |
if (startDate > endDate) { | |
var tmpDate = startDate; | |
startDate = endDate; | |
endDate = tmpDate; | |
} | |
while (startDate < endDate) { | |
dateRange.push(formatDate(startDate)); | |
startDate.setDate(startDate.getDate() + 1); | |
} | |
dateRange.push(formatDate(startDate)); | |
return dateRange; | |
} |
This file contains 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
{ | |
"name": "urbanhire-date", | |
"version": "1.0.0", | |
"description": "date range", | |
"main": "index.js", | |
"scripts": { | |
"test": "mocha" | |
}, | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"mocha": "^5.0.1" | |
} | |
} |
This file contains 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
var assert = require('assert'); | |
var u = require('./'); | |
describe('Date Range', function() { | |
it('should return expected responses', function() { | |
var dateRange = u.genDateRange('22 January 2018', '30 January 2018'); | |
assert.equal(dateRange.length, 9); | |
assert.equal(dateRange[0], '2018-01-22'); | |
assert.equal(dateRange[dateRange.length - 1], '2018-01-30'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment