Ad
Functional Programming
Algorithms
Mathematics
Code
Diff
  • export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => {
      return driverSpeeds.map(speed => CalculateFine(speed, speedLimit));
    }
    
    const CalculateFine = (speed: number, limit: number) => {
      const limitToFineArr = [
        { threshold: 30, fine: 500}, 
        { threshold: 20, fine: 250 },    
        { threshold: 10, fine: 100 }, 
      ];
      
      return Math.max(...limitToFineArr
              .map((obj, i) => (speed >= limit + obj.threshold) ? obj.fine : 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 limitToFineArr = [
    • { threshold: 30, fine: 500},
    • { threshold: 20, fine: 250 },
    • { threshold: 10, fine: 100 },
    • ];
    • return Math.max(...limitToFineArr
    • .map((obj, i) => (speed >= limit + obj.threshold) ? obj.fine : 0));
    • }