简体   繁体   中英

Absolute value Javascript

I am trying to create a MPL (multiple price list.) on otree (using PyCharm). For one of my app (WTP) I wrote this code on the html "folder" (using Javascript):

{% if choice.0 > 0 %}  If you are paid {{choice.0}} points for this item, would you accept?
    {% elif choice.0 == 0 %}
        If you are paid {{choice.0}} points for this item, would you accept?
    {% elif choice.0 < 0 %}
        If you have to pay {{choice.0}} points for this item, would you accept?
    {% endif %}

the different choices being defined on pages.py:

  self.participant.vars['choices'] = [
        [+30, "choice1", "choice1-yes", "choice1-no"],
        [+20, "choice2", "choice2-yes", "choice2-no"],
        [+10, "choice3", "choice3-yes", "choice3-no"],
        [0, "choice4", "choice4-yes", "choice4-no"],
        [-10, "choice5", "choice5-yes", "choice5-no"],
        [-20, "choice6", "choice6-yes", "choice6-no"],
        [-30, "choice7", "choice7-yes", "choice7-no"],

However, I would like all the numbers to appear without the "minus" sign on the participants' screen. How can I modify my Javascript code st -10 becomes 10, -20 becomes 20 and -30 becomes 30.

Thanks!

使用Math.abs()

Math.abs(-30) // 30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM