Skip to content

Instantly share code, notes, and snippets.

@kylebakerio
Last active August 29, 2015 14:13
Show Gist options
  • Save kylebakerio/e5f49adfc8aed57f1e76 to your computer and use it in GitHub Desktop.
Save kylebakerio/e5f49adfc8aed57f1e76 to your computer and use it in GitHub Desktop.
Maker Square Algorithms Challenge
/*Instructions:
Submit a solution to the following challenge:
Given an array of integers, find the smallest difference between any two elements of the array. For example:
var findSmallestDifference = function(arr) {
// Your code goes here
};
var result = findSmallestDifference([100, 500, 300, 1000, -200, 990]);
console.log(result); // The answer is 10 for this example because the difference between 1000 and 990 is 10
This code should print out 10 because the different between 1000 and 990 is 10 and there are no pairs that have a smaller difference.
---------------------------- */
var differences = [], differences2 = [], findSmallestDifference = function(arr) {
//first part creates a large array composed of the answers from subtracting every number against itself.
for(a=0;a<arr.length;a++){
for(b=a+1;b<arr.length;b++){
differences[differences.length] = arr[a] - arr[b];
differences[differences.length] = arr[b] - arr[a];
}
};
//second part ignores negative numbers and adds only positive numbers into a new array, differences2.
for (c=0;c<differences.length;c++){
if (differences[c] < 0) {}
else {
differences2[differences2.length] = differences[c];
}
};
//this variable gives me a number that I can guarantee will be larger than the answer.
var answer = (differences2[0] + differences2[1])
//this part compares two numbers from differences2 at a time, saves the smaller one into a temporary variable(bestOfTwo), and then compares the smaller one against the smallest one found yet('answer', which we declated in the last line)--if 'bestOfTwo is smaller than 'answer', it replaces it; if not, it exits the loop and cycles through to repeat the process with a new set of numbers
for (d=0;d<differences2.length;d++){
if (differences2[d] < differences2[d+1]) {
var bestOfTwo = differences2[d]
if (bestOfTwo < answer) {
answer = bestOfTwo;
}
}
};
return answer;
};
var result = findSmallestDifference([100, 500, 300, 1000, -200, 990]);
console.log(result);
@taylor
Copy link

taylor commented Jan 12, 2015

Kyle Baker: I have 4 different 'for' loops iterating, I know I could integrate the second one with the first one, for instance, but that second loop i only like 4 lines--part of me wonders if it's more clean, readable, and even shorter code just to leave it as it is. Is that just because I'm not very good yet, though? Is having it more dense always ideal from a DRY standpoint?

Kyle Baker: The fourth 'for' loop, I'm sure it could be integrated and done at the same time as the beginning, somehow. This draft is truly just a result of me breaking down the problems into steps in my head. (Again: is it better to have code more easily comprehensible like this? Or should I try and make it denser if at all possible?)

@taylor
Copy link

taylor commented Jan 12, 2015

The question wording does not explicitly say so, but I assume you don't want to find the difference to itself. index 2 diff index 2. You are covering that.

You do want to show 0 if you have the same number for two unique elements (eg. [100,100,200])

I do see a bug where the result stays the same based on the following: if you use the same number twice and the result shows 0 and then change the array it stays 0.

js> var result = findSmallestDifference([100, 500, 300, 1000, -200, 990]);
js> result
10
js> result = findSmallestDifference([100, 500, 300, 1000, -200, 990,-205]);
5
js> result = findSmallestDifference([100, 500, 300, 1000, -200, 990,-205,100]);
0
js> result = findSmallestDifference([100, 500, 300, 1000, -200, 990,-205]);      
0

@taylor
Copy link

taylor commented Jan 12, 2015

Steps:

  • Make it work
  • Make it beautiful
  • Make it fast

Feel free to re-factor after you feel it works.

Re-factor a small bit at a time and test that the results continue to work.

@taylor
Copy link

taylor commented Jan 12, 2015

Make sure it makes senses fully before re-factoring. Eg. don't condense if you do not yet comprehend what is going on.

@kylebakerio
Copy link
Author

If you add
console.log(a,b)
inside the second 'for' loop, you'll see that I'm actually not indexing 2, 2 as you suggested (I think, if I'm understanding you right).

Also, as per my comments in text, I can't seem to replicate the bug you describe. Is it because of the environment you're running the code in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment