arsalandywriter.com

How to Purchase a Car Using JavaScript: A DevAdvent Challenge

Written on

Chapter 1: Introduction to the Challenge

The DevAdvent 2022 challenge presented an intriguing problem focused on determining the intersection point of two curves. While the mathematics involved were not overly complex, the challenge lay in interpreting the results effectively. Let's delve into the specifics of the problem.

Section 1.1: The Problem Statement

Scenario: Purchasing a Car

A man owns an older vehicle valued at $2000 and has his sights set on a secondhand car priced at $8000. He plans to retain his current car until he has saved enough to purchase the new one. He estimates that he can save $1000 each month; however, both the value of his old car and the price of the new car depreciate by 1.5% monthly. Additionally, this depreciation rate increases by 0.5% every two months, complicating his savings calculations.

Can you assist him in figuring out how long it will take to save enough money to buy the desired car and how much he will have left afterward?

Function Parameters and Return:

  • start_price_old: (Positive float/int) The price of the old car.
  • start_price_new: (Positive float/int) The price of the new car.
  • saving_per_month: (Positive float/int) Monthly savings amount.
  • percent_loss_by_month: (Positive float/int) The monthly depreciation percentage.

For instance, calling nbMonths(2000, 8000, 1000, 1.5) should yield [6, 766], indicating he can purchase the new car in 6 months with $766 remaining.

Example Breakdown:

  • End of Month 1: Percent loss: 1.5% | Amount available: -4910.0
  • End of Month 2: Percent loss: 2.0% | Amount available: -3791.7999
  • End of Month 3: Percent loss: 2.0% | Amount available: -2675.964
  • End of Month 4: Percent loss: 2.5% | Amount available: -1534.06489
  • End of Month 5: Percent loss: 2.5% | Amount available: -395.71327
  • End of Month 6: Percent loss: 3.0% | Amount available: 766.158120825

Thus, the final return would be [6, 766] — 6 months to save up, and approximately $766 remaining after the purchase.

Section 1.2: Implementation of the Solution

My Approach

Here’s one way to structure the solution:

export function nbMonths(params) {

while (getMoneyLeft(params) < 0) {

params = { ...updateValues(params) };

}

return [params.month, Math.round(getMoneyLeft(params))];

}

Alternatively, a recursive approach could be utilized:

export function nbMonths(params) {

const moneyLeft = getMoneyLeft(params);

if (moneyLeft >= 0) {

return [params.month, Math.round(getMoneyLeft(params))];

} else {

return nbMonths(updateValues(params));

}

}

To keep the code organized, I opted to separate the problem into smaller functions, enabling clearer logic flow.

Section 1.3: Breaking Down the Functions

To improve readability and manageability, I defined a parameter object:

type Param = {

priceOld: number;

priceNew: number;

saving: number;

percentLoss: number;

month: number;

};

Next, I implemented a function to calculate the updated prices of the two cars:

const value = (price, delta) => price * (1 - delta / 100);

Subsequent functions update the prices and the loss percentage based on the current month:

const updatePriceNew = (p) => value(p.priceNew, p.percentLoss);

const updatePriceOld = (p) => value(p.priceOld, p.percentLoss);

const updatePercentLoss = (p) => p.percentLoss + (1 - (p.month % 2)) * 0.5;

const updateMonth = (p) => p.month + 1;

Finally, I created a function to calculate how much money remains after purchasing the car:

const getMoneyLeft = (p) => p.month * p.saving + p.priceOld - p.priceNew;

The final assembly of these components leads us back to the complete solution.

Chapter 2: Practical Application of the Solution

Video Title: JavaScript on Exercism 007 - Vehicle Purchase

This video provides insights into solving the car purchase challenge with JavaScript, demonstrating how to approach similar programming problems effectively.

Video Title: Create & Deploy a Responsive Car Website Design Using HTML CSS & JavaScript

Learn how to design a responsive car website utilizing HTML, CSS, and JavaScript, which can serve as a practical application of the concepts discussed here.

Thank you for reading! Stay tuned for more insightful content and programming challenges. Don't miss my next article—subscribe to my Medium email list for updates. Explore additional resources at PlainEnglish.io. Follow us on social media for more programming insights.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

The Path to Becoming a Spacefaring Civilization: A Future Vision

Exploring our need to become a spacefaring civilization while addressing Earth’s challenges.

Find Serenity: Harnessing the Wisdom of Martial Arts Philosophy

Explore the ancient wisdom of martial arts philosophy to cultivate inner peace and clarity in a chaotic world.

Unleashing Creativity Through the Power of Scents

Explore how various scents can enhance creativity and focus, along with tips and related resources.

Electric Vehicle Infrastructure: The Road Ahead for Kansas

Exploring the current state and future needs of electric vehicle charging infrastructure in Kansas and rural America.

Boosting Memory Through Dynamic Physical Activities

Exploring how dynamic physical activities like tree climbing can enhance working memory.

Empowering Leadership from Every Level: Insights from Maxwell

Discover how John C. Maxwell's

Exploring the Concept of a Bubble Universe: A Scientific Inquiry

Delve into the theory of the bubble universe and its implications within the multiverse concept.

Navigating Relationships with Difficult Neighbors: A Practical Guide

Discover effective strategies for managing challenging neighbor relationships while maintaining peace in your community.