Ad
Functional Programming
Algorithms
Mathematics

The local police force has placed a radar along a dirt road that they've recieved complaints about some folks speeding along the road that has the possibility of putting themselves and others at risk for injury. The radar will clock the driver's speed and record it in an array for later processing of the fines.

If the driver's speed is 10km/h to 19km/h above the speed limit, the fine will be 100 euros, if it is exceeded by 20km/h to 29km/h the fine will be 250 euros and if it is exceeded by more than 30km/h the fine will be 500 euros.

All input will be non-negative integer values.

Calculate the fine of each driver based on the speeds recorded in the array, and return an array of the fine amounts.

E.g.
array: [50,45,100,42]
limit: 50
return value: [0,0,500,100]

Code
Diff
  • 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;
      } else if (speed > limit + 20 && speed < limit + 29) {
        return 250;
      } else if (speed > limit + 10 && speed < limit + 19) {
        return 100;
      } else {
        return 0;
      }
    }
    • import java.util.*;
    • export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => {
    • return driverSpeeds.map(speed => CalculateFine(speed, speedLimit));
    • }
    • class Vehicle {
    • final int SPEED_LIMIT = 60;
    • int currentSpeed;
    • public void setCurrentSpeed(int[] accelerations) {
    • this.currentSpeed = Arrays.stream(accelerations).sum();
    • }
    • public int getCurrentSpeed() {
    • return this.currentSpeed;
    • const CalculateFine = (speed: number, limit: number) => {
    • if(speed > limit + 30) {
    • return 500;
    • } else if (speed > limit + 20 && speed < limit + 29) {
    • return 250;
    • } else if (speed > limit + 10 && speed < limit + 19) {
    • return 100;
    • } else {
    • return 0;
    • }
    • public boolean isWithinSpeedLimit() {
    • return getCurrentSpeed() <= this.SPEED_LIMIT;
    • }
    • }
    • }