3 Replies Latest reply on Oct 30, 2017 12:50 PM by sinious

    Struggling with writing Javascript if/else statement.

    Aiguste Level 1

      Hello everyone,

      So I am a complete beginner at Javascript and I am quite struggling with one of my coursework. I need to write a function which would count the total cost of dress hire taking into account a dress type, cost per day and number of days. I had to use radio buttons for the selection of dress type while days will be entered by someone who will use the website. I've started writing a function but it would be great if you could tell me if I'm going to the right direction.

      <script language="javascript">

        function calculate(){

        var price, days;

        var days = getField("days").value;


        if(document.myform.dresstype[0].checked == true)

        price = 60 + (15*days);

        else if (document.myform.dresstype[1].checked == true)

        price = 90 + (30*days);

        else if (document.myform.dresstype[2].checked == true)

        price = 120+ (40*days);

        else price = 0;






      I'm sure probably it's quite silly question, but thanks for your help


        • 1. Re: Struggling with writing Javascript if/else statement.
          try67 MVP & Adobe Community Professional

          Is this for a PDF file or a web-site? Because the code you posted is for the latter, but this forum is about the former...

          • 2. Re: Struggling with writing Javascript if/else statement.
            Aiguste Level 1

            Sorry! It is actually for the website.

            • 3. Re: Struggling with writing Javascript if/else statement.
              sinious Most Valuable Participant

              Be careful about multi-line statement blocks without using braces, such as your if/else statements. I don't know how you're calculating the price (as in what 30/60/120, 15/30/40 etc) refers to but using your sample, here's an example of calculating it on a page:



              <select id="dressType">
              <option value="0">Dress 1</option>
              <option value="1">Dress 2</option>
              <option value="2">Dress 3</option>
              <label for="dressDays" size="3"> for # of days:</label>
              <input type="number" value="30" id="dressDays" name="dressDays">
              <button id="calcBtn">Calculate</button>
              <p>Price: <input type="text" size="6" id="priceValue" value="0" readonly></p>



              document.getElementById('calcBtn').onclick = calculate;
              function calculate( evt ) {
              // get selected dress
                var dress = document.getElementById('dressType').value; 
                var days = document.getElementById('dressDays').value; 
                var price = 0;
                switch ( dress ) {
                case "0":
                  price = 60 + ( 15 * days );
                  case "1":
                  price = 90 + ( 30 * days );
                  case "2":
                    price = 120 + ( 40 * days );
                    price = 0;
                document.getElementById('priceValue').value = price.toString();
                console.log( 'Days, Dress, Price', days, dress, price );


              JSFiddle Example


              It's just using a switch statement instead of a series of if statements but either is fine. I used a switch statement since you appear to want to avoid the braces. The calculate button has a click event bound to the calculate function to produce the result in the price input. Hopefully it does what you want or leads you down the right path. Sorry for the late reply.