DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Public Solving: Checking the sleighs automatically
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Public Solving: Checking the sleighs automatically

The elves build and try a lot of different sleighs for Santa. Because of the number of sleighs, they are looking for an automated report.

Click here to view the original puzzle.

Each sleigh is already being tested, so they have the data available.
It's up to us to check if each system check is passing. If so, we should return a specific letter.

The checks pass if a value is above 90%.
The result should be an alphabetically sorted string.

If all checks fail, we should return an X.

Creating the sleigh system check

I won't describe the solution for this one but rather walk you straight through my implementation.

The sleigh has multiple attributes, but they are not sorted.
So I decided to start with a checkMap object.
This object will keep all the checks in alphabetical order and contain their letter value.

const checkMap = {
  accelerometer: 'A',
  breaks: 'B',
  compass: 'C',
  gyroscope: 'G',
  humiditySensor: 'H',
  langdingSuspension: 'L',
  navigation: 'N',
  pressureSensor: 'P',
  temperatureSensor: 'T',
  windSensor: 'W',
};
Enter fullscreen mode Exit fullscreen mode

I want to loop over these checks and add a letter if the value passes the inspection.

Once again, I'll be using the reduce method.

We have to extract the object keys so we can loop over them.

const checks = Object.keys(checkMap).reduce((output, check) => {
 // Do check
}, '');
Enter fullscreen mode Exit fullscreen mode

The check is actually the easy part as a value is valid if over 90%.

If that is the case, I add a letter to the output array.

const checks = Object.keys(checkMap).reduce((output, check) => {
    if (sleigh[check] >= 0.9) {
      output += checkMap[check];
    }
    return output;
}, '');
Enter fullscreen mode Exit fullscreen mode

What happens here is that if the sleighs check for the current check is above 0.9 (90%), we add the letter for that check to our array.

Now we just need a check if this checks string is empty.
If so, all checks must have failed, and we should return an X.

return checks.length ? checks : 'X';
Enter fullscreen mode Exit fullscreen mode

Let's see how we did by running the test.

Test turning green

That's it!
We got there, and we can now safely evaluate all the sleighs.

Do let me know what you would do differently in this solution.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (3)

Collapse
 
lexlohr profile image
Alex Lohr • Edited on

I'd use

Object.entries(checkMap).reduce(
  (result, [check, letter]) => 
    sleigh[check] >= 0.9
    ? `${result}${letter}`
    : result,
  ''
)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dailydevtips1 profile image
Chris Bongers

Just missing the X then, but definitely a nice one!
You must love the fact that I used a object map πŸ‘€

Collapse
 
lexlohr profile image
Alex Lohr

Yes, I think you've grokked it now. Using objects effectively like this can help making your logic more passive and thus more reliable.

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!