/*
Objective:
Fix gaps in sales data.
Method:
The gaps in the sales data (skipped dates missing from the data set) will be
filled in with "[date] + tab + undefined"
Loop through every line of data and if the date from the previous line doesnt equal the increment from the getDate() function, add the date.
*/
let data = prompt();
// split by tab: \t
data = (data.split("\n"))
let newData = "";
// loop through each item in data, check if the getDate + [one day] from the previous item equals the getDate from the current item. if it doesn't, add lines of ([date] + tab + undefined) until the date from the current item is reached.
function difference(date1, date2) {
// Define the reference date (day 0)
var referenceDate = new Date(date1); // January 1, 1970
// Calculate the difference in milliseconds between the given date and the reference date
var differenceInMilliseconds = date2.getTime() - referenceDate.getTime();
// Convert milliseconds to days
var dayNumber = differenceInMilliseconds / (1000 * 3600 * 24);
// Return the calculated day number
return Math.round(dayNumber);
}
function futureDay(inputDate, days) {
// Create a new Date object based on the input date
var nextDay = new Date(inputDate);
// Increment the date by one day
nextDay.setDate(nextDay.getDate() + days);
// Return the date that is one day later
var year = nextDay.getFullYear();
var month = ('0' + (nextDay.getMonth() + 1)).slice(-2); // Adding 1 because getMonth() returns zero-based month
var day = ('0' + nextDay.getDate()).slice(-2);
// Concatenate components into a string in the format "YYYY-MM-DD"
var dateString = month + '/' + day + '/' + year;
// Return the formatted date string
return dateString;
}
for (let i = 0; i < data.length-1; i++) {
let diff = difference(new Date(data[i].split("\t")[0]), new Date(data[i+1].split("\t")[0]));
if (diff != 1) {
for (let j = 0; j < diff; j++) {
newData +=...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.