Gabriele Ferro Home

Dev Advent Calendar – Day #06

Yesterday Santa Claus wanted us to write a function that generates random bags of sweets, and today we have to help him writing his list! So we need to write a program that checks a list of kids and analyze whether they were naughty or nice, so that Santa knows if they deserve candy and toys, or coal...

I don't know if it was intentional but the list of kids was not 'easy' to import. So I found a workaround.

Here is my code with the output.

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const kids = require("../data/sampleData.json");
export const findOutIfNaughtyOrNice = kid => {
    let score = 0;
    kid.events.forEach(event => {
        score += event.effect;
    });
    return score < 0 ? "naughty" : "nice"
}
export const getKids = () => {
    return kids;
}
export const getNiceKids = kids => {
    const niceKids = [];
    let score = 0;
    kids.forEach((kid) => {
        kid.events.forEach(event => {
            score += event.effect
        }); 
        if (score >= 0) { niceKids.push(kid); }
        score = 0;
    })
    return niceKids;
}
export const getNaughtyKids = kids => {
    const naughtyKids = [];
    let score = 0;
    kids.forEach((kid) => {
        kid.events.forEach(event => {
            score += event.effect
        }); 
        if (score < 0) { naughtyKids.push(kid); }
        score = 0;
    })
    return naughtyKids;
}

HOW TO PARTICIPATE

  1. Click the participation link
  2. Clone your repository
  3. Solve the puzzle
  4. Test your solution
  5. Add your Twitter handle
  6. Git push your changes

July 2024
MTWTFSS
1234567
891011121314
15161718192021
22232425262728
293031 

Archives

Gabriele Ferro Home
Copyright © Ferro Gabriele. All rights reserved.