• Status: Solved
• Priority: Medium
• Security: Public
• Views: 347

# 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;
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.
``````
0
capturetheflag
• 2
• 2
2 Solutions

Commented:
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%?
0

Owner (Aidellio)Commented:
See here for a demo: http://jsbin.com/oJiVOMe/2/edit

I removed the 'use strict' and it works ok now.  the document object isn't available in the script mode.
0

Author 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
0

Owner (Aidellio)Commented:
Sorry I took it your code was calculating correctly! can never assume anything :)

http://jsbin.com/oJiVOMe/3/edit

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;
}
``````
0

Author Commented:
Thanks for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.