Construct/Calculation Problem with Java Script file


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;
            case 'premium':
                cost = 15.00;
            case 'gold':
                cost = 20.00;
            case 'platinum':
                cost = 25.00;
        } // 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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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%?
RobOwner (Aidellio)Commented:
See here for a demo:

I removed the 'use strict' and it works ok now.  the document object isn't available in the script mode.
capturetheflagAuthor Commented:
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
RobOwner (Aidellio)Commented:
Sorry I took it your code was calculating correctly! can never assume anything :)

just added a line to multiply the number of members to the cost.

    // Discount multiple members:
    if (members > 1) {
      cost *= 0.90; // 90% or 10% off for multiple members
      cost *= members;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
capturetheflagAuthor Commented:
Thanks for the help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.