admin管理员组

文章数量:1315252

I have a series of radio buttons on my page that, when checked, establish certain conditions. For each condition, a value is subtracted from the PRICE, which is the final variable that is provided to the user.

if (condition1==0){price-=5;}
if (condition2==1){price-=4;}       
if (condition3==1){price-=10;}
var minimum = 1;
if (price < minimum){price = minimum;)

The conditions are all working exactly as I want. For this code group, I basically don't want "price" to go below 1. My problem is that this is creating an error and I am really not sure why. I am more familiar with Java, so perhaps I am running into an error that Javascript throws that I am not familiar with.

What can I do to make sure that my "price" variable does not fall below my "minimum" variable, without throwing an error?

I have a series of radio buttons on my page that, when checked, establish certain conditions. For each condition, a value is subtracted from the PRICE, which is the final variable that is provided to the user.

if (condition1==0){price-=5;}
if (condition2==1){price-=4;}       
if (condition3==1){price-=10;}
var minimum = 1;
if (price < minimum){price = minimum;)

The conditions are all working exactly as I want. For this code group, I basically don't want "price" to go below 1. My problem is that this is creating an error and I am really not sure why. I am more familiar with Java, so perhaps I am running into an error that Javascript throws that I am not familiar with.

What can I do to make sure that my "price" variable does not fall below my "minimum" variable, without throwing an error?

Share Improve this question asked Dec 29, 2011 at 17:59 benny.bennettbenny.bennett 7101 gold badge10 silver badges25 bronze badges 6
  • Have you tried replacing the variable name "minimum" with "minprice"? Just a hunch. – Eugen Rieck Commented Dec 29, 2011 at 18:03
  • Telling us the error is vital in order to help – sciritai Commented Dec 29, 2011 at 18:03
  • Please bring the error that shows up. – Alfabravo Commented Dec 29, 2011 at 18:03
  • It's a simple typo, there should be a curly brace at the end of the last line, not a parentheses. i.e if (price < minimum){price = minimum;} – ankit Commented Dec 29, 2011 at 18:04
  • I can think of no reason that this shouldn't work, besides the fact that you have no closing curly brace around price=minimum :-/ – Nick Coelius Commented Dec 29, 2011 at 18:05
 |  Show 1 more ment

3 Answers 3

Reset to default 4

You have a parentheses ) after price = minimum; rather than a curly bracket } which is why your code is creating an error.

Here is the changes.

Before:

if (price < minimum){price = minimum;)

After:

if (price < minimum){price = minimum;}
                                     ^

You could do something like this:

if (condition1 === 0) { price = Math.max(price - 5, 1); }

Or generalize it into a function:

function reducePriceBy(amount) {
    return Math.max(price - amount, 1); 
} 

And then

if (condition1 === 0) { price = reducePriceBy(5); }

You've got a syntax error to start: if (price < minimum){price = minimum;}

The last parentheses should have been a brace.

本文标签: Establishing a minimum value for Javascript variableStack Overflow