DEV Community

Cover image for Daily Code 76 | Speed Limit
Gregor Schafroth
Gregor Schafroth

Posted on

Daily Code 76 | Speed Limit

hi everyone! after a longer break i am back again with a small daily exercise. it’s simple but i think the different solutions are interesting. why don’t you give it a try as well? 😄

task

use javascript to write a function that determines the result of you driving at certain speeds:

  • speed limit: 120km/h (result: 'ok')
  • for every 5km/h above speed limit you get 1 point (result: 'x points'
  • if you get more than 12 points your licence is suspended (result: 'license suspended')

template:

function checkSpeed(speed){
  ...
}
Enter fullscreen mode Exit fullscreen mode

below are different solutions:

my solution

let speed = 130;
console.log(checkSpeed(speed));

function checkSpeed(speed) {
  const speedLimit = 120;
  const kmPerPoint = 5;
  const points = Math.floor((speed - speedLimit) / kmPerPoint)
  if (points <= 0) return 'ok';
  if (points >= 12) return 'License suspended'
  if (points == 1) return '1 point'
  return `${points} points`
}
Enter fullscreen mode Exit fullscreen mode

teacher solution (code with mosh)

checkSpeed(125);

function checkSpeed(speed) {
  const speedLimit = 120;
  const kmPerPoint = 5;

  if (speed < speedLimit + kmPerPoint)
    console.log('Ok');
  else {
    const points = Math.floor((speed - speedLimit) / 5)
    if (points >= 12)
      console.log('License suspended');
    else
      console.log('Points', points)
  }
}
Enter fullscreen mode Exit fullscreen mode

chatgpt solution

function checkSpeeding(speed) {
  const speedLimit = 120;
  const pointsPerExcessKm = 1;
  const pointsThreshold = 12;

  if (speed <= speedLimit) {
      return 'ok';
  } else {
      const excessSpeed = speed - speedLimit;
      const points = Math.floor(excessSpeed / 5) * pointsPerExcessKm;

      if (points > pointsThreshold) {
          return 'license suspended';
      } else {
          return `${points} points`;
      }
  }
}

// Example usage:
console.log(checkSpeeding(130)); // Output: '2 points'
console.log(checkSpeeding(140)); // Output: '4 points'
console.log(checkSpeeding(160)); // Output: 'license suspended'
console.log(checkSpeeding(115)); // Output: 'ok'
Enter fullscreen mode Exit fullscreen mode

conclusion

i am actually surprised that i like my solution better than the ones from mosh and chatgpt. feels much simplere and more straight forward. or am i missing something?

Some thongs when I compare the solutions:

  • mosh
    • instead of console.logging the function output, he just logs directly inside of the function. i don’t particularly like that because it causes him to repeat the console.log statement several times and it causes the function to not return anything.
    • i think the if (speed < speedLimit + kmPerPoint) console.log('Ok'); is really unintuitive. to me it’s much more logical to just give the ‘ok’ if points are 0 (hence I do it like that in my solution)
  • chatgpt

    • i really like the ‘example usage’ in the end. looks like there is again a lot of repeated code with all these log statements but still that’s something i also want to do going forward
    • chatgpt took an extra step to calculate the excessSpeed, which i just combined all in my points calculation. i think it’s a great approach to always do only one calculation per step to make things clearer, so that’s something i want to do as well going forward.
    • surprisingly chatgpt does not give the desired results from 121-124km/h (it should be ‘ok’ since there are no points), but i guess that should have been more clearly specified in the task, since 0 points can also be seen as a valid result in these cases.

    it’s a very simple exercise but still some nice learnings from me here. did you try it? how do you feel about these solutions? would love to hear your thoughts :)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more