Link to home
Start Free TrialLog in
Avatar of capturetheflag
capturetheflag

asked on

Construct/Calculation Problem with Java Script file

Hello,

I am having problems with the java script code below.  I want the calculation to multiply by the number of members, and then give the members 10% off for multiple memberships. Also give them an extra 10% off for any membership of 3 years or more. Here is my code and thanks for the help.
// membership.js
// This script calculates the cost of a membership.

// Function called when the form is submitted.
// Function performs the calculation and returns false.
function calculate() {
    
    // Be strict:
    'use strict';

    // Variable to store the total cost:
    var cost;

    // Get a reference to the form elements:
    var type = document.getElementById('type');
    var years = document.getElementById('years');
    var members = document.getElementById('members');

    
    // Convert the year to a number:
    if (years && years.value) {
        years = parseInt(years.value, 10);
    }
    
        // Convert the members to a number:
    if (members && members.value) {
        members = parseInt(members.value, 10);
    }
    
    // Check for valid data: 
   if (type && type.value && years && (years > 0) ) {
        
        // Determine the base cost:
        switch (type.value) {
            case 'basic':
                cost = 10.00;
                break;
            case 'premium':
                cost = 15.00;
                break;
            case 'gold':
                cost = 20.00;
                break;
            case 'platinum':
                cost = 25.00;
                break;
        } // End of switch.
        
        // Factor in the number of years:
        cost *= years;
        
        // Discount multiple years:
        if (years > 2) {
            cost *= .90; // 90% or 10% off for membership 3 years or more
        }
        
        // Discount multiple members:
        if (members > 1) {
            cost *= .90; // 90% or 10% off for multiple members
        }
        
        // Show the total amount:
        document.getElementById('cost').value = '$' + cost.toFixed(2);
        
    } else { // Show an error:
        document.getElementById('cost').value = 'Please enter valid values.';
    }
    
    // Return false to prevent submission:
    return false;
    
} // End of calculate() function.

// Initial setup:
function init() {
    'use strict';
    document.getElementById('theForm').onsubmit = calculate;
} // End of init() function.
window.onload = init;

Open in new window

Avatar of Gary
Gary
Flag of Ireland image

Cannot see anything wrong, where are you seeing it go wrong?
Only thing that is confusing is do you want 10% applied after already taking 10% off or should it be a total of 20%?
SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of capturetheflag
capturetheflag

ASKER

Hello tagit,

Thanks for displaying the code.  I did run into a problem. When I ran the code for Years = 1, and Members = 2, and a Basic type, I got $9.00. Should it be $18? 10% 0ff for each $10 membership? thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help!