Skip to content
Advertisement

implement a javascript function following certain rules

🙋‍♂️ I have an assessment in javascript here it is:

Goal:

In Chinese culture, it is common during celebrations to give “red envelopes” containing a little money. Most often, the adult generations give to the younger generations. You want to build a wechat application to help grandparents share their donation budget between their grandchildren.

Write a program that calculates the number of “lucky gifts” (equal to 8) according to the money budget and the number of giftees grandchildren

Functioning:

Many rules, mixing tradition and superstition, frame this gift:

Donations should not contain amount 4, as it sounds like “dead” it is favorable to donate an amount of 8, as it sounds like “fortune” it would be disapproved not to give anything to one of the grandchildren your algorithm must return the number of donations equal to 8 while respecting the following rules:

Spend the entire budget (unless there is enough budget to give everyone 8) Give no 4 (by tradition, the budget will never be 4) Give no 0 (unless the budget is not sufficient) Score a maximum of 8 once the above rules are respected implementation:

implement the function luckyMoney(money,giftees) which :

take as inputs the integers money and giftees with:

0 <=money< 100

0 <=giftees<10

and returns the number of donations equal to 8 as an integer

JavaScript

So I went ahead and implement the function as follows:

JavaScript

I think my code is wrong

What do you think, please?

Advertisement

Answer

A recursive solution might be easiest.

JavaScript

Alternatively, here is a non-recursive version with a loop. There may be a non-looping pure math solution that would be simpler, but I’m not sure what it would be.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement