Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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

 function luckyMoney(money,giftees) {
//write your code here
// to debug : console.error('Debug messages...");

return -1;

}

So I went ahead and implement the function as follows:

function luckyMoney(money,giftees){
  if (money % 8 ===0){
        return (money/8)
        }else if(money%4===0){
        return 0}

}

I think my code is wrong

What do you think, please?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A recursive solution might be easiest.

function luckyMoney(money, giftees) {
    if (money === 4) {
        throw new Error("You cannot give 4");
    }
    if (money >= giftees * 8) {  // There is enough to give everyone 8
        return giftees;
    }
    if (money < 8 + giftees - 1 || money === 12) { // There is not enough to give anyone 8, or giving someone 8 would mean the next person gets 4 (unlucky) or someone will get 0 (not allowed)
        return 0;
    }
    // Otherwise, the number of eights must be one greater than if we gave (money - 8) to (giftees - 1)
    return 1 + luckyMoney(money - 8, giftees - 1);
}

const testCases = [[8, 1, 1], [8, 2, 0], [12, 2, 0], [13, 2, 1], [13, 3, 1], [16, 2, 2],  [100, 10, 10], [100, 13, 11]];
for (const [money, giftees, expectedResult] of testCases) {
   result = luckyMoney(money, giftees);
   console.log(money, giftees, expectedResult, result);
}

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.

function luckyMoney(money, giftees) {
    if (money >= giftees * 8) {
        return giftees;
    }
    let r = 0;
    while (money >= 8 + giftees - r && money !== 12) {
        r++;
        money -= 8;
    }
    return r;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda