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
3 Answers
Reset to default 4You 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
版权声明:本文标题:Establishing a minimum value for Javascript variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741962198a2407344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论