Skip to content

Instantly share code, notes, and snippets.

@aldy505
Last active December 6, 2021 08:03
Show Gist options
  • Save aldy505/be8b27bd19e5b828963135002e616c1a to your computer and use it in GitHub Desktop.
Save aldy505/be8b27bd19e5b828963135002e616c1a to your computer and use it in GitHub Desktop.
A port of my Go's 14 days challenge into Javascript
// Hello again. This time, we will be exploring the
// Date class and object.
//
// The function that we'll make is pretty simple,
// we are going to have a date input, in the form (or type)
// of Date, then we'll add 14 days to it.
//
// But here's the tricky part:
// We'll skip satuday and sundays, so the entire 14 days
// should be on weekdays only, not on weekends.
//
// This is the pseudocode:
//
// var date = given date
// var days = 0
// var output = date
//
// while (days < 14)
// var dateInDay = convertToDay(output)
// switch dateInDay
// case "saturday"
// output = output + 1 day
// continue
// case "sunday"
// output = output + 1 day
// continue
// default
// output = output + 1 day
// days += 1
// end
// end
//
// return output
//
// But this pseudocode has a problem:
// What if the date stops at weekends?
// There's your challenge!
//
// Good luck!
function Add14Days(date) {
let output;
// Do things here!
return output;
}
console.log("Oh, hello there! Let's test your code.");
const tests = [
{
i: new Date(2021, 12, 1, 0, 0, 0, 0),
e: new Date(2021, 12, 21, 0, 0, 0, 0),
},
{
i: new Date(2021, 11, 1, 0, 0, 0, 0),
e: new Date(2021, 11, 18, 0, 0, 0, 0),
},
{
i: new Date(2021, 11, 30, 0, 0, 0, 0),
e: new Date(2021, 12, 20, 0, 0, 0, 0),
}
]
for (let j = 0; j < tests.length; j++) {
const {i, e} = tests[j];
const r = Add14Days(i);
if (r !== e) {
console.log(`--- Test ${j} failed. Expected ${e}, got ${r}`);
} else {
console.log(`--- Test ${j} passed.`);
}
}
console.log('The test is completed.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment