Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created October 25, 2025 17:39
Show Gist options
  • Select an option

  • Save tatsuyax25/ed837ccd67626f28fee6f7a6d437d518 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/ed837ccd67626f28fee6f7a6d437d518 to your computer and use it in GitHub Desktop.
Hercy wants to save money for his first car. He puts money in the Leetcode bank every day. He starts by putting in $1 on Monday, the first day. Every day from Tuesday to Sunday, he will put in $1 more than the day before. On every subsequent Monday,
/**
* @param {number} n
* @return {number}
*/
// Function to claculate the total amount of money saved over 'n' days
var totalMoney = function(n) {
let total = 0; // Initialize total money saved
let weekDay = 1; // Start from the first day of the week
let weekNumber = 1; // Start from the first week
// Loop over each day
for (let day = 1; day <= n; day++) {
total += weekDay + weekNumber - 1; // Add the day's money to the total
weekDay++; // Move to the next day
// Check if the week is complete
if (weekDay > 7) {
weekDay = 1; // Reset to the first day of the next week
weekNumber++; // Move to the next week
}
}
return total; // Return the total money saved
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment