Created
October 12, 2020 17:27
-
-
Save comster/ad5baee43294b79deb15f647deaf9e15 to your computer and use it in GitHub Desktop.
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
// | |
// Author: Jeff Pelton | |
// | |
// | |
// PROMPT: https://podcastindex.social/@dave/105022919584538996 | |
// | |
// Anybody want a code challenge? | |
// I'd like a script,function,method where I can pass it an array of unix timestamps and have it hand back an array of days of the week marking as true the days this podcast publishes episodes on. | |
// Like: | |
// findPublishDays([1602482305, 1602238118, ...]) | |
// Might return: { 0: true, 1: false, 2: false, 3: false, 4: true, 5: false, 6: false } | |
// Where index 0 is Sunday. This would indicate that the podcast updates on Sunday and Thursday. | |
// If no pattern is found, return false. | |
// | |
// SOLUTION: | |
// | |
// Instead of returning an object with 7 keys, I've juse used an array with 7 indexes | |
// It could be optimized/abbreviated, but it might be useful to look at the number of occurences | |
// | |
let inputDates = [1602482305, 1602238118] | |
const findPublishDays = function(dates) { | |
let daysOfWeekOccurences = [0, 0, 0, 0, 0, 0 ,0] | |
dates.forEach(function(date){ | |
let d = new Date(date * 1000) // convert from seconds to ms | |
daysOfWeekOccurences[d.getDay()]++; | |
}) | |
let daysWithShow = daysOfWeekOccurences.map(function(x){ | |
return x > 0 | |
}) | |
return daysWithShow | |
} | |
console.log(findPublishDays(inputDates)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment