-
-
Save kylebakerio/e5f49adfc8aed57f1e76 to your computer and use it in GitHub Desktop.
/*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
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
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.
Make sure it makes senses fully before re-factoring. Eg. don't condense if you do not yet comprehend what is going on.
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?