Random code I'm sharing for no good reason

This post is more than 2 years old.

Ok, the title should be your warning. I'm only posting this because it is Sunday night and I'm bored. I'm working on a demo for my jQuery video (did I mention I'm working on a jQuery video?) that mimics a typical car dealership inventory search. As we just upgraded our car I'm pretty familiar with these. The demo will focus on building the UI to create a search engine that can filter cars by model, trim, color, price, and features. In order to actually have something to search against, I wrote a script that creates an inventory of cars. Here is that script. Enjoy.

 * Returns a random integer between min and max
 * Using Math.round() will give you a non-uniform distribution!
function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;

/* A utility to create my car data.

We have models Models have trims (which they all share) cars have a color and an array of features from a list of possible ones */

var models = ["Alpha", "Beta", "Gamma", "Delta", "Epsilon"]; var trims = ["XT", "XTE", "Super", "Ultimate"]; var colors = ["Red", "Blue", "Silver", "Gold", "Black", "White"]; var features = ["Air Conditioning", "Cruise Control", "Backup Camera", "Power Steering", "Internet", "Sat Radio", "4WD", "Moon Roof"];

var inventory = [];

/* Ok, loop over each model. Each model will have 5-20 (rnd) of each trim. Each car will have a random color. Each car will have a random chance of having a feature with higher trims having a better chance of having it. Each car will have a price btn 20K-40K with each level of trim adding +10k to make them, normally, higher

*/ for(var i=0; i < models.length; i++) { var model = models[i]; for(var k=0; k < trims.length; k++) { var trim = trims[k]; var numberToAdd = getRandomInt(8, 23); console.log("Going to make "+numberToAdd+" "+model+" "+trim);

	for(var j=0; j&lt;numberToAdd; j++) {
		var car = {
		car.color = colors[getRandomInt(0, colors.length-1)];
		car.price = getRandomInt(20000, 40000) + (k*10000);
		car.features = [];
		for(var z=0; z&lt;features.length; z++) {
			if(getRandomInt(1,10) + k &gt; 6) {
				car.price += (car.features.length+1)*1000;


//so i can see it console.table(inventory); //so i can copy it console.log(JSON.stringify(inventory));

In case you're curious, I'm going to write a simple module that wraps calls to this data so that I don't have to use an application server to serve it up.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Pete Williamson posted on 11/10/2014 at 4:40 PM

I build websites for cars. I spent last week "debating" with my boss and the data manager over which word to use to describe air conditioning, sat nav etc.

I kept saying "features", they disagreed... seeing this makes me so happy:

var features = ["Air Conditioning", "Cruise Control", "Backup Camera", "Power Steering", "Internet", "Sat Radio", "4WD", "Moon Roof"];

Regarding your code, that's a lot of nested IFs... even for demo code isn't it nicer to use functions and callbacks.

Comment 2 by Raymond Camden posted on 11/10/2014 at 5:08 PM

I thought about breaking it out into a function to randomly add features, etc, but, decided to just KISS. As it stands, *this* code won't be in the preso.

Comment 3 by Dan Kraus posted on 11/10/2014 at 10:11 PM

@Pete - I'm not in the car business, just a passing interest in them, but I think they're commonly referred to as "options" in the business. Ex - The GT trim comes with or without a moonroof. It's optional. A "feature" would be something like say power windows because it's standard in that trim. Every car in that trim has it.

So they end up being two different things. Some options are sometimes only available in certain trims too.

It's probably outside the scope of your demo, Ray, and it gets the point across already :)

Comment 4 by Pete Williamson posted on 11/10/2014 at 10:41 PM

@Dan yeah you're right we use the word "option" for optional equipment, and I guess that's what Ray was listing - I was thinking of the overall grouping for all options (or standards) of a type

E.g. feature: special paint, could be pearlescant paint, metallic paint, matte paint etc
or feature: leather seats, could be part leather, full leather, hand stitched leather etc

Anyway like you said, the demo is perfectly clear, and automotive terminology is an absolute nightmare.

@Ray you could add this to your MockData projects, give the random people more properties or just a different flavor of data

Comment 5 by roger tubby posted on 11/10/2014 at 11:28 PM

And wouldn't it be nice to do this with just some mapping functions - no for() loops a-tall.

Comment 6 by Raymond Camden posted on 11/11/2014 at 12:04 AM

Well, I could map over some of it, not all of it. :P