Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Javascript Help PLEEAASSEE!

  • 07-04-2004 2:40pm
    #1
    Registered Users, Registered Users 2 Posts: 565 ✭✭✭


    hey this is an exam question im studying for my 1st year javascript exam
    i have the code working fine but my prob is in the 3rd and 4th bullet point i have just
    coded one function - How do u code a second function and call the arguments to the parameters x and y in the function PrintCost()

    i would REALLY appreciate some help

    attached is a jpeg of the question and here is the code i have written so far



    <html>

    <title>Car Pricing Calculator ¦ IS1103 ¦ Summer 2002</title>

    <head>

    <script language = "javascript">

    var fiesta,regular,sunset,blue,sunroof,numvat,numbasic_cost,numcost_inc_vat;


    function calculate() //function to calculate cost
    {


    if(document.cars.purchase[0].checked){
    regular = 0;
    }



    if(document.cars.purchase[1].checked){
    sunset = 350;
    }
    else{
    sunset = 0;
    }


    if(document.cars.purchase[2].checked){
    blue = 275;
    }
    else{
    blue = 0;
    }


    if(document.cars.purchase[3].checked){
    sunroof = 225;
    }
    else{
    sunroof = 0;
    }



    intregular = parseInt(regular);
    intsunset = parseInt(sunset);
    intblue = parseInt(blue);
    intsunroof = parseInt(sunroof);
    vat = parseInt(numvat);
    basic_cost = parseInt(numbasic_cost);
    cost_inc_vat = parseInt(numcost_inc_vat);

    basic_cost = (9500 + intregular + intsunset + intblue + intsunroof);
    vat = (basic_cost * 0.21)
    cost_inc_vat = (basic_cost + vat);

    alert("Basic Cost : " + basic_cost + " Euro")
    alert("Cost including VAT @ 21% : " + cost_inc_vat + " Euro")

    } //end of calculate function


    </script>

    </head>

    <body>

    <table border = "1">

    <tr>
    <td>DESCRIPTION</td>
    <td>COST</td>
    </tr>

    <tr>
    <td>Ford Fiesta Flight</td>
    <td>9500 Euro</td>
    </tr>


    <form name = "cars">
    <tr>
    <td><input type = "radio" value = "0" name = "purchase">Regular Black</td>
    <td>Free</td>
    </tr>

    <tr>
    <td><input type = "radio" value = "1" name = "purchase">Sunset Red</td>
    <td>350 Euro</td>
    </tr>

    <tr>
    <td><input type = "radio" value = "2" name = "purchase">Midnight Blue</td>
    <td>275 Euro</td>
    </tr>

    <tr>
    <td><input type = "checkbox" value = "3" name = "purchase">Sunroof</td>
    <td>225 Euro</td>
    </tr>

    </form>

    <tr>
    <td>
    <center>
    <input type="button" name = "submit" value="Calculate Cost" onclick="calculate()";
    </center>
    </td>
    </tr>

    </table>

    </body>
    </html>


Comments

  • Moderators Posts: 6,900 ✭✭✭Spocker


    Firstly the help bit :D

    To call the new function PrintCost() passing the two parameters x and y use the following line:

    PrintCost(x,y)

    somewhere after the alert("Cost... tag - this will call the function. Then, between the lines } //end of calculate function and </script> write your new code:

    function PrintCost(x,y) {
    // do something here...
    }

    And now for the bad news :(
    The code as it's written won't work properly in all cases - click calculate when no radio buttons on boxes are ticked. The alert message box has your text and the letters NaN - it means Not a Number. Consider redeclaring your variables in some other fashion.


    Good luck! :cool:


  • Registered Users, Registered Users 2 Posts: 5,335 ✭✭✭Cake Fiend


    Originally posted by Dr. Spock
    use the following line:

    PrintCost(x,y)

    somewhere after the alert("Cost... tag

    Werl, I'd imagine the object of the last two bullet points would be to get him to make the PrintCost() function itself do the alerting. I.e. the alerts would be in the function like so:

    function PrintCost(x,y)
    {
    alert(blah blah)
    }

    and the function would be called like so:

    PrintCost(basic_cost, cost_inc_vat);

    I don't write javascript though, this is only portable observation...

    Edit: I've just noticed something: The reason you get NaN is because you don't initialize the variable 'regular' if it's not the first option. You don't need the first if statement, as regular should always be 0. The way you have it now, if you don't run through the program at least once with 'Regular Black' selected, then this line:

    basic_cost = (9500 + intregular + intsunset + intblue + intsunroof);

    tries to add an uninitialized variable to integers - hence 'Not a Number'.


  • Registered Users, Registered Users 2 Posts: 1,268 ✭✭✭hostyle


    Just cause I'm bored, and never got to have a JavaScript exam:
    <html>
    
    <title>Car Pricing Calculator &#166; IS1103 &#166; Summer 2002</title>
    
    <head>
    
    <script language = "javascript">
    
    function CalcCost(myform)  {
    
    
    	var initial_cost;
    
    	for (var i=0; i < myform.elements.length; i++) {
    		if (myform.elements[i].name == "purchase" && myform.elements[i].checked) {
    			initial_cost = parseInt(myform.elements[i].value);
    		}
    	}
    
    	if (typeof(initial_cost) != "undefined") {
    
    		var sunroof = (myform.sunroof.checked) ? parseInt(myform.sunroof.value) : 0;
    	
    		basic_cost = (9500 + initial_cost + sunroof);
    		cost_inc_vat = (basic_cost + (basic_cost * 0.21));	
    	
    		alert("Basic Cost : " + basic_cost + " Euro")
    		alert("Cost including VAT @ 21% : " + cost_inc_vat + " Euro")
    
    	} else {
    		alert("Please select a car type");
    	}
    
    
    }
    
    
    </script>
    
    </head>
    
    <body>
    
    <table border=1>
    
    <tr>
    <td>DESCRIPTION</td>
    <td>COST</td>
    </tr>
    
    <tr>
    <td>Ford Fiesta Flight</td>
    <td>9500 Euro</td>
    </tr>
    
    
    <form name="cars" onsubmit="CalcCost(this); return false">
    
    <tr>
    <td><input type = "radio" value = "0" name = "purchase">Regular Black</td>
    <td>Free</td>
    </tr>
    
    <tr>
    <td><input type = "radio" value = "350" name = "purchase">Sunset Red</td>
    <td>350 Euro</td>
    </tr>
    
    <tr>
    <td><input type = "radio" value = "275" name = "purchase">Midnight Blue</td>
    <td>275 Euro</td>
    </tr>
    
    <tr>
    <td><input type = "checkbox" value = "225" name = "sunroof">Sunroof</td>
    <td>225 Euro</td>
    </tr>
    
    <tr>
    <td colspan=2 align="center">
    <input type="submit" name = "submit" value="Calculate Cost">
    </td>
    </tr>
    
    </form>
    
    </table>
    
    </body>
    </html>
    


Advertisement