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<numberToAdd; j++) {
var car = {
model:model,
trim:trim
};
car.color = colors[getRandomInt(0, colors.length-1)];
car.price = getRandomInt(20000, 40000) + (k*10000);
car.features = [];
for(var z=0; z<features.length; z++) {
if(getRandomInt(1,10) + k > 6) {
car.features.push(features[z]);
car.price += (car.features.length+1)*1000;
}
}
inventory.push(car);
}
}
}
//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.
Archived Comments
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.
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.
@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 :)
@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
And wouldn't it be nice to do this with just some mapping functions - no for() loops a-tall.
Well, I could map over some of it, not all of it. :P