switch-case with many conditions

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


switch-case with many conditions



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


switch


if statement


if statement


switch


function sumUpNewCar




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 standartLoan = Number (document.getElementById("newAuto2").innerHTML);
var promoPlusLoan = Number (document.getElementById("newAuto3").innerHTML);
var standartPlusLoan = 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 sumStandartLoansBonus = Number (document.getElementById("sumStandartLoansBonus").innerHTML);
var sumStandartPlusLoansBonus = Number (document.getElementById("sumStandartPlusLoansBonus").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));
sumStandartLoansBonus = Math.round (Number (standartLoan * 0.0321));
sumStandartPlusLoansBonus = Math.round (Number(standartPlusLoan * 0.0432));
break;

case thirtyCompleted.checked:
sumStandartLoansBonus = Math.round (Number (standartLoan * 0.0321));
sumStandartPlusLoansBonus = Math.round (Number(standartPlusLoan * 0.054321));
break;

case thirtyFiveCompleted.checked:
sumPromoLoansBonus= Math.round (Number(promoLoan * 0.01234));
sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.0321));
sumStandartLoansBonus= Math.round (Number (standartLoan * 0.066));
sumStandartPlusLoansBonus = Math.round (Number(standartPlusLoan * 0.7888));
break;

case sprCompleted.checked:
sumPromoLoansBonus = Math.round (Number(promoLoan * 0.01222));
sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.028989));
sumStandartLoansBonus = Math.round (Number (standartLoan * 0.02111));
sumStandartPlusLoansBonus = Math.round (Number(standartPlusLoan * 0.041111));
break;

case sprCompleted.checked && twentyFiveCompleted.checked:
sumStandartLoansBonus = Math.round (Number (standartLoan * 0.047868));
sumStandartPlusLoansBonus= Math.round (Number(standartPlusLoan * 0.056555));
break;

case sprCompleted.checked && thirtyFiveCompleted.checked:
sumPromoLoansBonus = Math.round (Number(promoLoan * 0.02222));
sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.0345));
sumStandartLoansBonus = Math.round (Number (standartLoan * 0.04445));
sumStandartPlusLoansBonus= Math.round (Number(standartPlusLoan * 0.0667);
break;

default:
sumPromoLoansBonus = Math.round (Number (promoLoan * 0.0015));
sumPromoPlusLoansBonus= Math.round (Number (promoPlusLoan * 0.0244));
sumStandartLoansBonus = Math.round (Number (standartLoan * 0.0244));
sumStandartPlusLoansBonus = Math.round (Number (standartPlusLoan * 0.03511));
break;
}


}

function calculate(){
var sumPromoLoansBonus = Number (document.getElementById("sumPromoLoansBonus").innerHTML);
var sumPromoPlusLoansBonus = Number (document.getElementById("sumPromoPlusLoansBonus").innerHTML);
var sumStandartLoansBonus = Number (document.getElementById("sumStandartLoansBonus").innerHTML);
var sumStandartPlusLoansBonus = Number (document.getElementById("sumStandartPlusLoansBonus").innerHTML);
var sumNewVehicle = document.getElementById("sumNewVehicle").innerHTML = Number (sumPromoLoansBonus + sumPromoPlusLoansBonus + sumStandartLoansBonus + sumStandartPlusLoansBonus );


<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="standart" 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="standartPlus" 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="sumStandartLoansBonus"></td> <!-- sum of standart box BASIC-->
<td id="sumStandartPlusLoansBonus"></td> <!-- sum of standart plus box BASIC-->
</table>





What have you triedd so far? Have you debugged your code?
– user743414
33 mins ago




2 Answers
2



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
}


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived