Posted on Leave a comment

This morning’s programming challenge

Within the next 2 hours, write a piece of javascript that based upon values in a series of pulldowns determines the price of a product using quantity, an attribute, another attribute, a third attribute, and the size of the product. There is not a mathematical basis for the price.

Update: The solution follows. This is viewable through the source otherwise I wouldn’t post it. Basically we create a class/object to hold the pricing data. When then create an array of objects to hold each permutation of pricing. Then we make a call to the database and fill the array. Finally, whenever a selection box is changed, we loop through the selections comparing values to what is stored in our objects until we find the one that has the price we want. Then we update the price field. The final product can be seen shortly at Marqiprinting.com under the products listings. Right now flyers works correctly and I’m updating code for the other products.


//define a class to hold the data
function productinfo(qty, coating, sides, tag, productsize, price) {
//initialize object properties
    this.quantity = qty;
    this.uvcoating = coating;
    this.sides = sides;
    this.tag = tag;
    this.productsize = productsize;
    this.price = price;
}

//create an array to hold the objects of data
var products = new Array();

<?php
    $x=0;
    while($line=mysql_fetch_array($productsresourceid)){
       echo "products[".$x."] = new productinfo(".$line['Qty'].",".$line['Coating'].",".$line['NumSides'].",".$line['Tagline'].",'".$line['AvailableSize']."',".$line['ProductPrice'].");";
       $x++;
    }
?>

function updateprice(){
    var pricefound = false;

    for(i=0; i < products.length; i++) {
       //test values here
       frm = document.productselection;
       //businescards = ProductSize, ProductSides, Coating, qty, tag
       //brochures = ProductSize, ProductSides, Coating, qty, tag
       //cdinserts = ProductSize, Coating, qty, tag, ProductSides
       //flyers = ProductSize, ProductSides, Coating, qty, tag
       //posters = ProductSize, ProductSides, Coating, qty, tag
      if(document.getElementById('ProductSize').options[document.getElementById('ProductSize').selectedIndex].value == products[i].productsize
          && document.getElementById('ProductSides').options[document.getElementById('ProductSides').selectedIndex].value == products[i].sides
          && document.getElementById('Coating').options[document.getElementById('Coating').selectedIndex].value == products[i].uvcoating
          && document.getElementById('qty').options[document.getElementById('qty').selectedIndex].value == products[i].quantity
          && document.getElementById('tag').options[document.getElementById('tag').selectedIndex].value == products[i].tag) {
      updatedivs('price','Price: $'+products[i].price.toFixed(2));
       pricefound = true;
       alert('price found! '+products[i].price);
       }
    }
    if(!pricefound)       updatedivs('price','Price: $----');
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.