What is wrong with this code?

Discussion in 'JavaScript' started by mcfc4eva, Mar 30, 2009.

  1. #1
    var i=0;
    var count=0;
    var cost=0;	
    do
    {
    	do
    	{
    		if (insPrice[i]==cost){
    			document.write(i+" - "+insName[i]+" - &pound;"+insPrice[i]+"<br/>");
    			count=count+1;
    		}
    		i=i+1;
    	}
    	while(i<15);
    	cost=cost+1;
    }
    while(count<15);
    
    Code (markup):
    insPrice and insName arrays are both declared earlier in the script and include 15 entries each.
    insPrice consists of numbers and insName consists of strings.

    The purpose of the script is to list insPrice in ascending (lowest to highest) order with the related insName.

    e.g.
    I was thinking about using the sort() function but realised that would jumble the order of insPrice() so it did not correspond to insName(). e.g. insPrice[4] would not belong to insName[4].

    Can anybody point me in the right direction?

    Thanks,
    Mike
     
    mcfc4eva, Mar 30, 2009 IP
  2. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #2
    Put insName[] & insPrice[] in the same array and sort on the Price column.
     
    mmerlinn, Mar 31, 2009 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    further to the private message... here it is in action, with the sort fixes:
    http://fragged.org/dev/insurance.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Car Insurance Quotes</title>
    
    <script type='text/javascript'>
    var C = {
        // console wrapper by fragged.org
        debug: true, // global debug on|off
        quietDismiss: false, // may want to just drop, or alert instead
        log: function() {
            if (!C.debug) return false;
    
    
            if (typeof console == 'object' && typeof console.log != "undefined")
                console.log.apply(this, arguments);
            else
                if (!C.quietDismiss) {
                    var result = "";
                    for (var i = 0, l = arguments.length; i < l; i++)
                        result += arguments[i] + " ("+typeof arguments[i]+") ";
    
                    alert(result);
                }
        }
    } // end console wrapper.
    
    
    var insurers = {
        "BB": {
            name: "BB",
            url: "http://www.bar.foo",
            base: 1231,
            over25: 380,
            female: 34,
            mileage: 43,
            femaleMileage: 450,
            noclaims: {
                5: 85,
                more: 85
            }
        },
        "AA": {
            name: "AA",
            url: "http://www.theaa.co.uk",
            base: 1450 ,
            over25: 300,
            female: 20,
            mileage: 60,
            femaleMileage: 0,
            noclaims: {
                5: 100,
                more: 200
            }
        },
        "Kenstel Moon": {
            name: "Kenstel Moon",
            url: "http://www.foo.bar",
            base: 1165,
            over25: 475,
            female: 89,
            mileage: 97,
            femaleMileage: 180,
            noclaims: {
                5: 98,
                more: 98
            }
        }
        // etc etc
    }, getQuote = function(insurer, user) {
        var quote = insurer.base;
    
        if (user.Age >= 25)
            quote -= insurer.over25;
    
        if (user.Gender == "female")
            quote -= insurer.female;
    
        if (user.Mileage < 10000)
            quote -= insurer.mileage;
    
        if (user.Age >= 25 && user.Gender == "female" && user.Mileage < 10000)
            quote -= insurer.femaleMileage;
    
        if (user.NoClaims > 0) {
            var key = (user.NoClaims <= 5) ? 5 : "more";
            quote -= insurer.noclaims[key] * parseInt(user.NoClaims);
        }
    
        return quote;
    }
    
    // read
    var user = {
        Name: "dimitar",
        Age: 25,
        Gender: "male",
        Mileage: 10000,
        NoClaims: 2
    }, quotes = [];
    
    
    // to access data:
    // C.log(insurers["Kenstel Moon"]['name']);
    // C.log(insurers["Kenstel Moon"]['base']);
    for(x in insurers) {
        quotes.push({
            quote: getQuote(insurers[x], user),
            insurer: x
        });
    
        // C.log("quote for " + x + ": " + getQuote(insurers[x]));
    }
    
    var sortQuotes = function(a, b) {
        return a.quote - b.quote;
    }, showQuotes = function(quotes, where) {
        var output = "";
        for(x in quotes) {
            var insurer = insurers[quotes[x]["insurer"]];
            output += "<div><div style='float: left;width: 70px'>&pound;"+quotes[x]['quote'] + "</div><div style='float: left;width: 200px'>from " + quotes[x]["insurer"] +"</div><div style='float: left;'><a href='" + insurer.url + "'>"+insurer.url+"</a></div><br clear='all' /></div>";
        }
    
        where.innerHTML = output;
    };
    
    
    window.onload = function() {
        showQuotes(quotes, document.getElementById("output"));
        showQuotes(quotes.sort(sortQuotes), document.getElementById("output2"));
    }
    
    //C.log(quotes);
    //C.log(quotes.sort(sortQuotes));
    
    </script>
    </head>
    <body>
        Unsorted:
        <div id="output"></div><br />
    
        Sorted:
        <div id="output2"></div><br />
    
    </body>
    </html>
    PHP:
    i hope it makes sense now... but the sorted quotes back references the insurers hash and polls its data, then outputs it on screen. good luck with the assignment, i REALLY hate doing anything to do with insurance quotations, worst business to get into for webdev :)
     
    dimitar christoff, Mar 31, 2009 IP