Getting out of the "Code Sinkhole": A less crazy random number generator

Chatted with fnenu this morning about the state of my poor random number generator. I really built a monster from what was supposed to be a simple exercise. Welcome to programming, I suppose. Anyway, it FINALLY occurred to me that there was no need to calculate the difference between the max and min values, since the min value is subtracted from the max before being multiplied by Math.random().

And then I figured I should just use Math.max() and Math.min() to identify the upper and lower (inclusive) bounds for the set of possible numbers returned to the user. It feels a little weird applying them to a set of 2 numbers, but the code certainly does not suffer from the omission of extraneous if statements that I had in my previous version.

I think what this illustrates is, even "simple" coding problems can quickly take on a life of their own, and before you know it you're passing in conditionals and callbacks left and right. I think of this as falling into the "code sinkhole." If you sense yourself getting stuck down there, it might be a good time to get a snack or take a walk. Because you don't want to be in the code sinkhole for too long.

// TODO: deal with non-number inputs

// collect num1 from user
var num1 = parseInt(prompt("Give me a number!"));

// collect num2 from user
var num2 = parseInt(prompt("Give me a different number!"));

// do not accept num2 if it is equal to num1
while (num2 === num1){
  num2 = parseInt(prompt("Give me a DIFFERENT number!")); 

// find and store the min and max
var min = Math.min(num1, num2);
var max = Math.max(num1, num2);

// use Math methods to generate random num 
// store in variable to return to user
// need to start from the min (inclusive), rather than start from 0
var randomNum = Math.floor(Math.random() * (max - min + 1)) + min;

// alert the randomNum to user
alert("Your number is " + randomNum);