{"size": 1980, "ext": "js", "max_stars_repo_path": "scripts/data-parsing/solution.js", "max_stars_repo_name": "Hallbergs/node-sandbox", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "scripts/data-parsing/solution.js", "max_issues_repo_name": "Hallbergs/node-sandbox", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "scripts/data-parsing/solution.js", "max_forks_repo_name": "Hallbergs/node-sandbox", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.6956521739, "max_line_length": 79, "alphanum_fraction": 0.6181818182}
import { INPUT } from "./input.js";

export default class Solver {
  getNumUsersOverAge = (age) => {
    return INPUT.reduce((acc, user) => (user.age > age ? acc + 1 : acc), 0);
  };

  getLastRegisteredActiveUser = () => {
    return INPUT.reduce((acc, user) =>
      user.isActive && Date.parse(user.registered) > Date.parse(acc.registered)
        ? user
        : acc
    );
  };

  getNumFavoriteFruit = () => {
    const fruitCount = new Map();
    for (const user of INPUT) {
      const currentCount = fruitCount.get(user.favoriteFruit) || 0;
      fruitCount.set(user.favoriteFruit, currentCount + 1);
    }
    return fruitCount;
  };

  getMostCommonEyeColor = () => {
    const eyeColors = new Map();
    for (const user of INPUT) {
      const currentCount = eyeColors.get(user.eyeColor) || 0;
      eyeColors.set(user.eyeColor, currentCount + 1);
    }
    return [...eyeColors.keys()].reduce((a, b) =>
      eyeColors.get(a) > eyeColors.get(b) ? a : b
    );
  };

  getCombinedBalance = () => {
    const formatter = new Intl.NumberFormat("en-US", {
      style: "currency",
      currency: "USD",
      minimumFractionDigits: 2,
      maximumFractionDigits: 2,
    });

    return formatter.format(
      INPUT.reduce(
        (acc, user) => acc + Number(user.balance.replace(/[^0-9.-]+/g, "")),
        0
      )
    );
  };

  getUserNameFromId = (id) => {
    const user = INPUT.find((u) => u.id === id);
    return user ? `${user.name.last}, ${user.name.first}` : null;
  };
}

const solver = new Solver();

console.log("Age above 50: ", solver.getNumUsersOverAge(50));
console.log("Last registered: ", solver.getLastRegisteredActiveUser());
console.log("FruitCount: ", solver.getNumFavoriteFruit());
console.log("Most common eye color is: ", solver.getMostCommonEyeColor());
console.log("Combined balance: ", solver.getCombinedBalance());
console.log(
  "User with id: '5aabbca3e58dc67745d720b1': ",
  solver.getUserNameFromId("5aabbca3e58dc67745d720b1")
);
