0

I'm using switch construction instead of if statement because I have more than one repeated statements where if statement doesn't wrong.

But it couldn't be calculated properly. Where am I wrong? The switch construction is not working. I used the console and the function sumUpNewCar worked right.

Therefore, I would like to execute all the sums in the box

   function sprSelection() {
             var sprCompleted = document.getElementById("sprCompleted");
             var sprDoesntCompleted = document.getElementById("sprDoesntCompleted");

             if(sprCompleted.checked) {
                sprDoesntCompleted.disabled = true;
             }
             else if(sprDoesntCompleted.checked) {
                sprCompleted.disabled = true;
             }
             else {
                sprDoesntCompleted.disabled = false;
                sprCompleted.disabled = false;
             }
         }
   
   function prNewVehicleSelection (){
        var twentyFiveCompleted = document.getElementById("twentyFiveCompleted");
        var thirtyCompleted = document.getElementById("thirtyCompleted");
        var thirtyFiveCompleted = document.getElementById("thirtyFiveCompleted");

        if(twentyFiveCompleted.checked) {
            thirtyCompleted.disabled = true;
            thirtyFiveCompleted.disabled = true;
        }
        else if(thirtyCompleted.checked) {
            twentyFiveCompleted.disabled = true;
            thirtyFiveCompleted.disabled = true;
        }
        else if(thirtyFiveCompleted.checked) {
            twentyFiveCompleted.disabled = true;
            thirtyCompleted.disabled = true;
        }
        else {
            twentyFiveCompleted.disabled = false;
            thirtyCompleted.disabled = false;
            thirtyFiveCompleted.disabled = false;
        }
    }
    
    function sumUpNewCar(){
                var promoLoan = Number (document.getElementById("promoLoan").innerHTML);
                var standardLoan = Number (document.getElementById("newAuto2").innerHTML);
                var promoPlusLoan = Number (document.getElementById("newAuto3").innerHTML);
                var standardPlusLoan = Number (document.getElementById("newAuto4").innerHTML);
                var twentyFiveCompleted = document.getElementById("twentyFiveCompleted");
                var thirtyCompleted = document.getElementById("thirtyCompleted");
                var thirtyFiveCompleted = document.getElementById("thirtyFiveCompleted");
                var sprCompleted = document.getElementById("sprCompleted");
                var sprDoesntCompleted = document.getElementById("sprDoesntCompleted");
                var qualityCompleted = document.getElementById("qualityCompleted");
                var qualityDoesntCompleted = document.getElementById("qualityDoesntCompleted");
                

                var sumPromoLoansBonus = Number (document.getElementById("sumPromoLoansBonus").innerHTML);
                var sumPromoPlusLoansBonus = Number (document.getElementById("sumPromoPlusLoansBonus").innerHTML);
                var sumStandardLoansBonus = Number (document.getElementById("sumStandardLoansBonus").innerHTML);
                var sumStandardPlusLoansBonus = Number (document.getElementById("sumStandardPlusLoansBonus").innerHTML);
                var sumNewVehicle = Number (document.getElementById("sumNewVehicle").innerHTML);

                switch(sumNewVehicle) {
                    case twentyFiveCompleted.checked:
                    sumPromoLoansBonus = Math.round (Number(promoLoan * 0.0155));
                    sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.0289));
                    sumStandardLoansBonus = Math.round (Number (standardLoan * 0.0321));
                    sumStandardPlusLoansBonus = Math.round (Number(standardPlusLoan * 0.0432));
                    break;

                     case thirtyCompleted.checked:
                     sumStandardLoansBonus = Math.round (Number (standardLoan * 0.0321));
                     sumStandardPlusLoansBonus = Math.round (Number(standardPlusLoan * 0.054321));
                        break;

                    case thirtyFiveCompleted.checked:
                        sumPromoLoansBonus= Math.round (Number(promoLoan * 0.01234));
                        sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.0321));
                        sumStandardLoansBonus= Math.round (Number (standardLoan * 0.066));
                        sumStandardPlusLoansBonus = Math.round (Number(standardPlusLoan * 0.7888));
                        break;
                    
                    case sprCompleted.checked:
                        sumPromoLoansBonus = Math.round (Number(promoLoan * 0.01222));
                        sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.028989));
                        sumStandardLoansBonus = Math.round (Number (standardLoan * 0.02111));
                        sumStandardPlusLoansBonus = Math.round (Number(standardPlusLoan * 0.041111));
                        break;
                    
                    case sprCompleted.checked && twentyFiveCompleted.checked:
                        sumStandardLoansBonus = Math.round (Number (standardLoan * 0.047868));
                        sumStandardPlusLoansBonus= Math.round (Number(standardPlusLoan * 0.056555));
                        break;

                    case sprCompleted.checked && thirtyFiveCompleted.checked:
                        sumPromoLoansBonus = Math.round (Number(promoLoan * 0.02222));
                        sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.0345));
                        sumStandardLoansBonus = Math.round (Number (standardLoan * 0.04445));
                        sumStandardPlusLoansBonus= Math.round (Number(standardPlusLoan * 0.0667); 
                        break;

                    default:
                    sumPromoLoansBonus = Math.round (Number (promoLoan * 0.0015));
                     sumPromoPlusLoansBonus= Math.round (Number (promoPlusLoan * 0.0244));
                    sumStandardLoansBonus = Math.round (Number (standardLoan * 0.0244));
                    sumStandardPlusLoansBonus = Math.round (Number (standardPlusLoan * 0.03511));
                    break;
                }
               

                    }
                    
         function calculate(){
                var sumPromoLoansBonus = Number (document.getElementById("sumPromoLoansBonus").innerHTML);
                var sumPromoPlusLoansBonus = Number (document.getElementById("sumPromoPlusLoansBonus").innerHTML);
                var sumStandardLoansBonus = Number (document.getElementById("sumStandardLoansBonus").innerHTML);
                var sumStandardPlusLoansBonus = Number (document.getElementById("sumStandardPlusLoansBonus").innerHTML);
                var sumNewVehicle = document.getElementById("sumNewVehicle").innerHTML = Number (sumPromoLoansBonus + sumPromoPlusLoansBonus + sumStandardLoansBonus + sumStandardPlusLoansBonus );
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>revenue calculator Version 7</title>
</head>

<body>
    
    
    <!--BOX #1-->
   <div class="mainbox">

        <form >

            <input class="promo" type="number" min="1000" max="100000000" id="promoLoan" onchange="sumUpNewCar()"> 
            
            <input class="standard" type="number" min="100000" max="100000000" id="newAuto2" onchange="sumUpNewCar()"> 
         
            <input class="promoPlus" type="number" min="100000" max="100000000" id="newAuto3" onchange="sumUpNewCar()"> 

            <input class="standardPlus" type="number" min="100000" max="100000000" id="newAuto4" onchange="sumUpNewCar()">
        
        </form>

    </div> 

    <!--BOX #2-->
    <div class="mainbox2">


       <label class="container4"><b>25%</b>
        <input type="checkbox" class="input1" id="twentyFiveCompleted" onclick="prNewVehicleSelection(), sumUpNewCar(), calculate()">
        </label>

        <label class="container4"><b>30%</b>
            <input type="checkbox" class="input1" id="thirtyCompleted" onclick="prNewVehicleSelection(), sumUpNewCar(), calculate()">
        </label>

        <label class="container4"><b>35%</b>
            <input type="checkbox" class="input1" id="thirtyFiveCompleted" onclick="prNewVehicleSelection(), sumUpNewCar(), calculate()">
        </label>

        <label class="container" for="yes"><b> Да</b>
        <input type="checkbox" class="input1" name="yes" id="sprCompleted" onclick="sprSelection(), sumUpNewCar()">
        </label>

        <label class="container" for="no"><b> Нет</b>
        <input type="checkbox" class="input1" name="no" id="sprDoesntCompleted" onclick="sprSelection(), sumUpNewCar()">
         </label>


         <!--BOX#6-->

         <div class="mainbox6"> 
             
   

        <table class="tableOverall" >

            <tr>
     
                <td id="sumNewVehicle"></td>
               
            </tr>

        </table>        
           
        </div> 
  
</div>
<table class="tableOverall2">
    <tr>
        <td id="sumPromoLoansBonus"></td> <!-- sum of promo box   BASIC-->
        <td id="sumPromoPlusLoansBonus"></td> <!-- sum of promoplus box  BASIC-->
         <td id="sumStandardLoansBonus"></td> <!-- sum of standard box  BASIC-->
        <td id="sumStandardPlusLoansBonus"></td> <!-- sum of standard plus box  BASIC-->
     </table>
      
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

What are the possible values that sumNewVehicle will has? It will return some number in your case.

Also what values that twentyFiveCompleted.checked will have? I guess it will give either True or False.

So if both these variables are having different values then your switch case will never work.

Secondly you are using "&&" condition in Switch case which is not accepted.

Even if twentyFiveCompleted.checked will return a Number then also due to "&&" condition in Switch case your switch case iteration won't work.

For example:- you are using

case sprCompleted.checked && twentyFiveCompleted.checked:

That is the case where you should use if clauses, instead of switch case.

It will work in JavaScript as long as your conditions return proper boolean values, but it doesn't have many advantages over else if statements.Your comparison value is an integer, but most of your case expressions resolve to a boolean value. Mainly those functions where you have used "&&" conditions.

Refer this link for more info javascript: using a condition in switch case

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Abhishek Sharma
  • 360
  • 1
  • 10
  • How should I figure it out? Before I used `switch`, I had worked with `if statement` and there was the same. When the statement started with "&&" conditions, which was repeated, I got stuck cause to `if statement` caught the first `true` and it didn't go further. Therefore, `switch` seemed better than `if`. – Daniel Nuevo Aug 01 '18 at 12:33
  • Hi Daniel! Yes switch is better than if. Can you use console.log or something similar, so that we can see the values for these variables? Which if statement you got stuck to? Kindly elaborate please. – Abhishek Sharma Aug 02 '18 at 09:00
  • namaste ji Abhishek! Which three value do you mean? Earlier, I had this code [link](https://codepen.io/danielnuevo/pen/ZjRJVq). And there code stopped on line 29 because the first true had already gotten. This is why I used `switch` after ` if statement` – Daniel Nuevo Aug 03 '18 at 17:40
-1

I think you need to check out the switch Case syntax :

switch(expression) {
case n:
    code block1
    break;
case m:
    code block 2
    break;
default:
    code block 3
}

which is equivelant to :

if (expression == n) {
  code block 1
}
else if (expression == m) {
  code block 2
}
else {
  code block 3
}