Challenge Story:
"7777...8?!??!", exclaimed Bob, "I missed it again! Argh!" Every time there's an interesting number coming up, he notices and then promptly forgets. Who doesn't like catching those one-off interesting mileage numbers?
Let's make it so Bob never misses another interesting number. We've hacked into his car's computer, and we have a box hooked up that reads mileage numbers. We've got a box glued to his dash that lights up yellow or green depending on whether it receives a 1 or a 2 (respectively).
It's up to you, intrepid warrior, to glue the parts together. Write the function that parses the mileage number input, and returns a 2 if the number is "interesting" (see below), a 1 if an interesting number occurs within the next two miles, or a 0 if the number is not interesting.
Note: In Haskell, we use No, Almost and Yes instead of 0, 1 and 2.
Challenge details:
Interesting numbers are 3-or-more digit numbers that meet one or more of the following criteria:
- Any digit followed by all zeros: 100, 90000
- Every digit is the same number: 1111
- The digits are sequential, incrementing†: 1234
- The digits are sequential, decrementing‡: 4321
- The digits are a palindrome: 1221 or 73837
- The digits match one of the values in the awesomePhrases array
† For incrementing sequences, 0 should come after 9, and not before 1, as in 7890
‡ For decrementing sequences, 0 should come after 1, and not before 9, as in 3210
Error Checking:
- A number is only interesting if it is greater than 99!
- Input will always be an integer greater than 0, and less than 1,000,000,000.
- The awesomePhrases array will always be provided, and will always be an array, but may be empty. (Not everyone thinks numbers spell funny words...)
- You should only ever output 0, 1, or 2.
const isInteresting = (n, awes) => {
if (n < 98) return 0;
if (n < 100) return 1;
const nums = [n, n + 1, n + 2];
for (let i = 0; i < nums.length; i += 1) {
const flipped = [];
let copy = nums[i];
let isSame = true;
let isDecr = true;
let isIncr = true;
for (copy; copy > 0; copy /= 10) {
const curr = copy % 10;
if (flipped.length) {
const prev = flipped[flipped.length - 1];
if (prev !== curr) isSame = false;
if (prev + 1 !== curr) isDecr = false;
if (!(prev - 1 === curr || prev + 9 === curr)) isIncr = false;
}
flipped.push(curr);
if (copy - curr === 0) {
// is awesome could technically be better placed at the beginning
// of the loop. Looks nicer here though.
const isAwes = awes.some(phrase => phrase === nums[i]);
const isSingl = Number(`${flipped.join('')}`) < 10;
const isPalin = Number(`${flipped.join('')}`) === nums[i];
const almostOrYes = i > 0 ? 1 : 2;
if (isSingl || isSame || isDecr || isIncr || isPalin || isAwes) return almostOrYes;
}
copy -= curr;
}
}
return 0;
};
About the code: Works and produces desired results
Why I want reviews: Going to begin looking into applying and am brushing up on general problem solving. Want to be as self aware as possible heading into interviews. I also feel fairly inadequate as a programmer so I will attempt to do these often in order to build up momentum.
Background:
I noticed that when I went to check other people's answers to this challenge it was mostly string manipulation and a lot of splitting code into their own functions (needed in some cases, not so much in others). This opinionated line of thinking made me realize I honestly should get feedback on my own code and get a better idea of what someone with more experience would think about both this solution in comparison to the "obvious" solution and as a standalone given the language features used.
What I need:
- An honest opinion about the code. Feel free to touch up on anything and everything that you feel is relevant. (You know, the usual review)
- Does this code raise any red flags or good points for a potential hire? (Ok if this is not enough to go by)
- I am uncertain if you can do this but if possible spoiler tag your code. (Or maybe even link to your code?)
- Any refactoring/personal takes on the challenge should be preceded with concepts that I can search for so I can maximize the value and maybe even attempt your solution before I look at your code.
- Any article, book, video presentation, podcast, etc. which you think might help me with my coding (if you feel I should know/be aware of something)
- How long should this have taken to come up with? (opinion)
- Is this code optimal?