Ad
Functional Programming
Algorithms
Mathematics
Code
Diff
  • export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => {
      const fines = [
        [30, 500],
        [20, 250],
        [10, 100],
      ];
      
      return driverSpeeds.map(speed => {
        const overLimit = speed - speedLimit;
        const fine = fines.find(([threshold]) => overLimit >= threshold);
        return fine ? fine[1] : 0;
      });
    }
    • export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => {
    • return driverSpeeds.map(speed => CalculateFine(speed, speedLimit));
    • }
    • const CalculateFine = (speed: number, limit: number) => {
    • if(speed >= limit + 30) return 500
    • if (speed >= limit + 20 && speed <= limit + 29) return 250
    • if (speed >= limit + 10 && speed <= limit + 19) return 100
    • return 0
    • const fines = [
    • [30, 500],
    • [20, 250],
    • [10, 100],
    • ];
    • return driverSpeeds.map(speed => {
    • const overLimit = speed - speedLimit;
    • const fine = fines.find(([threshold]) => overLimit >= threshold);
    • return fine ? fine[1] : 0;
    • });
    • }