3 Christmas Coding Challenges — for you!

Josip Vojak
3 min readDec 29, 2022

Ho ho ho! Greetings, fellow coders! I am Santa Claus, and I need your help getting ready for Christmas and New Year’s Eve. My trusty helpers, the elves, have been working hard, but we could use a few extra hands. Are you up for the challenge?

I have prepared three coding challenges for you, all with a festive twist.

Photo by Toa Heftiba on Unsplash

1. “Santa’s Naughty or Nice List”

Problem:

Santa is getting ready for Christmas and wants to make sure he has a list of all the good and bad kids. Write a function isNice(name: string, actions: string[]) that takes a name and an array of actions as input and returns "nice" if the person has been good, and "naughty" if they have been bad.

Santa considers a person good if they have performed at least three good actions and no bad actions. A good action is any action that includes the word “kind” or “generous”. A bad action is any action that includes the word “mean” or “selfish”.

If the person has performed both good and bad actions, or if they have performed less than three good actions, they are considered “naughty”.

Example:

isNice("Alice", ["helped an old lady cross the street", "was kind to her little brother", "donated to a charity"])

Returns:

"nice"

Explanation:

To solve this problem, we can use the filter() function to count the number of good and bad actions, and then use an if statement to determine whether the person is "nice" or "naughty".

Here’s the solution:

function isNice(name, actions) {
const goodActions = actions.filter(action => action.includes("kind") || action.includes("generous"));
const badActions = actions.filter(action => action.includes("mean") || action.includes("selfish"));
if (goodActions.length >= 3 && badActions.length === 0) {
return "nice";
} else {
return "naughty";
}
}

2. “Santa’s Gift Wrapper”

Problem:

Santa is getting ready for Christmas and needs your help wrapping his gifts. Write a function wrapGift(gift: string, wrappingPaper: string) that takes a gift and a wrapping paper color as input and returns a string representing the wrapped gift.

The gift should be wrapped in a box with the specified wrapping paper color. The box should be topped with a ribbon of the same color as the wrapping paper.

The returned string should follow this format:

A [wrappingPaper] box with a [wrappingPaper] ribbon holds a [gift].

Example:

wrapGift("teddy bear", "red")

Returns:

"A red box with a red ribbon holds a teddy bear."

Explanation:

We can use string interpolation to insert the specified gift and wrapping paper color into the prescribed string format.

Here’s the solution:

function wrapGift(gift, wrappingPaper) {
return `A ${wrappingPaper} box with a ${wrappingPaper} ribbon holds a ${gift}.`;
}

3. “New Year’s Countdown”

Problem:

It’s almost New Year’s Eve, and you want to help your friends count down the days until the new year. Write a function countdown(currentYear: number, currentMonth: number, currentDay: number) that takes the current year, month, and day as input and returns the number of days until New Year's Eve.

New Year’s Eve is December 31st of the current year.

Example:

countdown(2022, 12, 29)

Returns:

2

Explanation:

This can be solve if we use the Date object to calculate the difference between the current date and New Year's Eve in milliseconds, and then divide that number by the number of milliseconds in a day to get the number of days.

Here’s the solution:

function countdown(currentYear, currentMonth, currentDay) {
const currentDate = new Date(currentYear, currentMonth - 1, currentDay);
const newYearsEve = new Date(currentYear, 11, 31);
const diff = newYearsEve - currentDate;
return Math.floor(diff / (1000 * 60 * 60 * 24));
}

I hope you had fun solving these coding challenges! Thank you for helping Santa and his elves get ready for Christmas and New Year’s Eve. Your skills and creativity have been a great asset to us.

I hope you have a very merry Christmas and a happy New Year! May your holiday season be filled with joy, laughter, and, of course, plenty of good cheer. Don’t forget to leave out some milk and cookies for Santa on Christmas Eve!

Until next year, farewell and happy coding!

Yours truly, Santa Claus

--

--

Software architect (AWS Certified) and a former professional volleyball player.