I'm creating a panel and there are stats for memory, CPU and HDD. I'm using a switch statement and in the case method, I'm putting the current usage of CPU, memory and HDD.
However, the problem is that I'm using operators and I don't know which operator to use because I've tried all of them and I didn't get the results that I expected.
And this is the code: https://pastebin.com/YaxCm0Be
switch(true){
case (mem_percent_get <= 0.01):
var mem_progress_color = 'progress-bar-primary';
break;
case (mem_percent_get <= 33):
var mem_progress_color = 'progress-bar-success';
break;
case (mem_percent_get <= 66):
var mem_progress_color = 'progress-bar-warning';
break;
case (mem_percent_get <= 80):
var mem_progress_color = 'progress-bar-danger';
break;
default:
mem_progress_color = 'progress-bar-theme';
}
switch(true){
case (cpu_percent_get <= 33):
var cpu_progress_color = 'progress-bar-success';
break;
case (cpu_percent_get <= 66):
var cpu_progress_color = 'progress-bar-warning';
break;
case (cpu_percent_get <= 80):
var cpu_progress_color = 'progress-bar-danger';
break;
default:
cpu_progress_color = 'progress-bar-primary';
}
switch(true){
case hdd_percent_get <= 0.01:
var hdd_progress_color = 'progress-bar-primary';
break;
case hdd_percent_get <= 30:
var hdd_progress_color = 'progress-bar-success';
break;
case hdd_percent_get <= 60:
var hdd_progress_color = 'progress-bar-warning';
break;
case hdd_percent_get <= 80:
var hdd_progress_color = 'progress-bar-danger';
break;
default:
hdd_progress_color = 'progress-bar-theme';
}
Well, my first comment is to not use a switch in this case. What you are doing is essentially if () { } else if() {} blocks. You should be using switch when you have a value that you want to strictly check against. I suggest looking into at the MDN docs for switch.
Secondly, from what I can gather is that for the memory, you need it to be red when the value is 1696 / 2098 (80.83%). All of your if/elseif cases rely on <= which would mean that the value must be less than or equal to the number on the right of the equation. In your case, you are looking for <= 80, and without seeing how you calculate mem_percent_get (if it is in the pastebin, I'm unable to open that on my current network), you're value is likely above 80.
For your danger, you likely want 80-100+% as being red, so you should be using >= or greater than or equal to operator.
MDN has an excellent resources on comparison operators.
Created a getClassName method that accepts a percent and will return a className:
const getClassName = percent => {
switch(true){
case (percent <= 0.01):
return 'progress-bar-primary';
case (percent <= 33):
return 'progress-bar-success';
case (percent <= 66):
return 'progress-bar-warning';
case (percent <= 80):
return 'progress-bar-danger';
default:
return 'progress-bar-theme';
}
}
console.log('0: ', getClassName(0));
console.log('40: ', getClassName(40));
console.log('50: ', getClassName(50));
console.log('80: ', getClassName(80));
console.log('100: ', getClassName(100));
Related
function texas(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
var answer = "low";
case 4:
case 5:
case 6:
var answer = "medium";
break;
} else if(val => 7) {
var answer = "Huge"
}
return answer;
}
it says error Declaration or statement expected. ts(1128) [13, 7]
and it poits at the else if statement
You can use the "default" keyword, but you should probably update your code in order to handle the cases in which the value of the parameter is not positive or not a number:
function texas(val) {
if (val <= 0 || isNan(val)) {
throw new InvalidOperationException("val should be a positive number");
}
switch(val) {
case 1:
case 2:
case 3:
return "low";
case 4:
case 5:
case 6:
return "medium";
default:
return "Huge"
}
}
It's >= and the elsehas to be deleted. The varfor answer is unnecesary, just declare it once with let. You forgot the break in case 3:.
function texas(val) {
let answer = "";
switch(val) {
case 1:
case 2:
case 3:
answer = "low";
break;
case 4:
case 5:
case 6:
answer = "medium";
break;
}
if(val >= 7) {
answer = "Huge"
}
return answer;
}
console.log(texas(2));
console.log(texas(8));
You just need to return in the switch
function texas(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
var answer = "low";
return answer;
case 4:
case 5:
case 6:
var answer = "medium";
return answer;
}
if(val => 7) {
var answer = "Huge"
}
return answer;
}
The syntax does not allow to put an else after a switch. else only makes sense in combination with an if statemen. But switch has a default: case which most closely matches your intention (hopefully):
function texas(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
var answer = "low";
break;
case 4:
case 5:
case 6:
answer = "medium";
break;
default:
if(val >= 7) {
answer = "Huge"
}
// decide what should happen if val is 0, -1 or not even a number (e.g. texas('gotcha!')
break;
}
return answer;
}
Don't forget to put break in your cases, otherwise execution will "fall through" and execute the next cases. You would never end up with "low"
You can't use an if statement within a switch block.
You do have the default option tho -
function texas(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
answer = "low";
case 4:
case 5:
case 6:
answer = "medium";
break;
default:
answer = val >= 7 ? "Huge" : "Invalid";
break;
}
return answer;
Note that if you have a minus / negative answer, it'll also fall into this clause, but you can the the value of answer with an inline ?: if statement...
You can't put the else after the switch block as people have stated above. switch statement is better for multi way branching and fixed data values. On the other side, if statement is better for boolean values. You can do something like this. It might not be the shortest line of codes, but just so you that there's another approach:
function texas(val) {
let answer = "";
switch (true) {
case (val == 1 || val == 2 || val == 3):
answer = "low";
break;
case (val == 4 || val == 5 || val == 6):
answer = "medium";
break;
case (val >= 7):
answer = "huge";
break;
}
return answer;
}
// It is simple code
var num = prompt("put number");
// This way is not worked
switch (num) {
case num > 0:
console.log("num++");
break;
case num < 0:
console.log(num-2);
break;
}
// But this worked
if (num > 0){
console.log(num++);
} else if (num < 0){
console.log(num -2);
}
My first way by "switch" is not worked but "if" method worked.
I tried all of thing for changing code or other ways but the same result.
Please guys help me.
Because the statement num > 0 inside you case will return true or false.
If you do this:
switch (true) {
case num > 0:
console.log("num++");
break;
case num < 0:
console.log(num-2);
break;
}
It will work.
Cases cannot be expressions, you must normalize your input first.
Although it is valid to place an expression in a case, in this scenario a more tried-and-true way of dealing with this is to first normalize your input first.
You can determine direction for example:
var num = parseInt(prompt("put number"), 10);
var direction = num < 0 ? -1 : 1;
switch (direction) {
case 1:
console.log("num++");
break;
case -1:
console.log(num - 2);
break;
}
The switch acts as a case switcher, meaning you cannot make comparisons to create cases, just list cases by case, and perform some function from this case. The if / else structure is suitable for making comparisons, as the expected result in the if call is always a boolean.
Example:
const a = 1;
if (a === 1) {
console.log('hello');
} else {
console.log('sad');
switch (a) {
case 1 : console.log('hello'); break;
default: console.log('sad'); break;
In your case, I recommend using if/else if/else, as it is more recommended.
I have a calculator code that i am using on my site which is not working the way i want it to work.
I have four plans all having different percentages given to different amounts
First Plan : Amount Limit from 10 to 500
Second Plan : Amount Limit from 501 to 1000
Third Plan : Amount Limit from 1001 to 1500
Fourth Plan : Amount Limit from 1501 to 2000
It expect it to work in such a way that when an amount is entered it should automatically switch to the corresponding plan
For Example if 1200 is entered it should automatically change to Third Plan.
$(function(){
calc();
enter code here
$('#calc_plan').on('change', calc);
$('#inv_amount').bind('change keyup', calc).on('keypress', isNumberKey);
});
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
function calc() {
var plan = $('#calc_plan').val();
var amount = $('#inv_amount').val();
var percent;
switch (plan) {
case '1':
switch (true) {
case (amount<=499):
percent = 104;
break;
case (amount<=999):
percent = 105;
break;
case (amount<=4999):
percent = 106;
break;
case (amount<=9999):
percent = 107;
break;
case (amount<=49999):
percent = 108;
break;
case (amount<=99999):
percent = 109;
break;
default:
percent = 109;
}
break;
case '2':
switch (true) {
case (amount<=499):
percent = 114;
break;
case (amount<=999):
percent = 117;
break;
case (amount<=4999):
percent = 120;
break;
case (amount<=9999):
percent = 123;
break;
case (amount<=49999):
percent = 126;
break;
case (amount<=99999):
percent = 129;
break;
default:
percent = 129;
}
break;
case '3':
switch (true) {
case (amount<=499):
percent = 124;
break;
case (amount<=999):
percent = 131;
break;
case (amount<=4999):
percent = 138;
break;
case (amount<=9999):
percent = 145;
break;
case (amount<=49999):
percent = 152;
break;
case (amount<=99999):
percent = 159;
break;
default:
percent = 159;
}
break;
case '4':
switch (true) {
case (amount<=499):
percent = 154;
break;
case (amount<=999):
percent = 175;
break;
case (amount<=4999):
percent = 196;
break;
case (amount<=9999):
percent = 217;
break;
case (amount<=49999):
percent = 238;
break;
case (amount<=99999):
percent = 259;
break;
default:
percent = 259;
}
break;
}
$('#assign_per').val(percent+'%');
var total = amount*percent/100;
$('#total_return').val(total+'$');
$('#net_profit').val((total-amount).toFixed(2)+'$');
}
According to what I understood from your question is that you want the amount to automatically select the plan based on the rule given whereas you are using variable for it.
function check= function(amount,a,b){
// check if the amount is in range a,b
return (amount>=a && amount < b);
}
function calc() {
var amount = $('#inv_amount').val();
var percent;
var plan = check(amount,10,500)?1:check(amount,500,1000)?2:check(amount,1000,1500)?3:0 (as much condition you wanna check)
switch (plan) {
case '1':
switch (true) {
case (amount<=499):
percent = 104;
break;
I'm writing swtich javascript switch statement in JS file and figured out the problem whole day still cannot find the solution.
Here is my javascript file written in jQuery :
var percent = 20;
var widthbytes;
switch(percent)
{
case 0:
widthbytes=0;
break;
case (percent > 10 && percent < 20):
widthbytes=16;
break;
case (percent >=20 && percent < 30):
widthbytes=30;
break;
default:
widthbytes=0;
break;
}
average.width(widthbytes);
It always return to default instead of 30. Anything wrong with my codes ?
switch statement only check the value of variable and then give the result according to that value so your expression
case (percent > 10 && percent < 20):
return boolean value which is not not comparable to variable value. Use if-else to get the job done.
just make a bit change in your code.
You have switch(percent)**in your code, only change for this ***switch(true)*.
The reason for that is because the switch statement return a boolean value, this is why we need they have the same comparation, i.e. boolean vrs boolean.
For example the case 10: return one value; true or false.
I can't see a problems with #Carlos Marin's answer. This works:-
var percent = 10; //test values-> 10, 11, 19, 20, 21, 29, 30
var widthbytes;
switch(true){
// case 0:
// widthbytes=0;
// break;
case (percent > 10 && percent < 20):
widthbytes=16;
break;
case (percent >=20 && percent < 30):
widthbytes=30;
break;
default:
widthbytes=0;
break;
}
console.log(widthbytes);
switch statements don't work like that. Your second case is checked like this: if (percent == (percent > 10 && percent < 20)) ..., which will not yield the desired result.
You could use an if / elseif / else construct:
if (percent === 0) {
widthbytes = 0;
} else if (percent > 10 && percent < 20 {
widthbytes = 16;
} else if (percent >= 20 && percent < 30 {
widthbytes = 30;
} else {
widthbytes = 0;
}
Or you could use a function that turns the ranges into constants:
function getRange(percent) {
return Math.floor(percent/10);
}
switch(getRange(percent)) {
case 10:
widthbytes = 16;
break;
case 20:
widthbytes = 30;
break;
default:
widthbytes = 0;
}
Note that to get a cleaner implementation i assimilated your original case 0: into the default, since they both do the same thing. If that is not desirable, you need to change the getRange function to no longer return the same range for 0 as for any number between 0 and 10.
So I play a RPG on a Virtual Tabletop that supports API. I do not want to create a bunch of objects individually, so I am working with the API, and want to read attributes from a JSON dump and then write those attributes to a object in the game (Character). So all goes well with my code so far as long as I am pulling static info. But in the case of Skills, they may or may not have all of the attr defined, so NULL. I would like to be able to identifiy the null and move on, not fail out because it is NULL.
I have attached a GIST, I am a n00b to this, and I am a System Engineer in real life, but not a coder. So I would appreciate the input!
https://gist.github.com/bigdadmike/7548421
Above is all of my code, but specifically this is the section I am working on, these have all been declared as var at top of script. I have updated this post with comments and suggestions so far from Basti. Code:
on('ready', function() {
_.each(monsterManual, function (monsterData){
log(monsterData.Name);
var character = createObj('character', {
name: monsterData.Name,
gmnotes: monsterData.FullText,
});
//*/
_.each(monsterAttributes, function(attr) {
var max = "";
var cur = "";
var re;
switch(attr){
case 'AC':
cur = parseInt(monsterData[attr].match(/(\d+)/)[1]);
break;
case 'Str':
case 'Dex':
case 'Con':
case 'Int':
case 'Wis':
case 'Cha':
re = new RegExp(attr + "\\s*(\\d*).*");
cur = parseInt(monsterData['AbilityScores'].match(re)[1]);
break;
case 'HD':
case 'Size':
case 'CR':
cur = monsterData[attr];
break;
case 'HP':
cur = parseInt(monsterData[attr]);
max = cur;
break;
case 'BaseAtk':
cur = parseInt(monsterData[attr]);
max = cur;
break;
case 'CMB':
cur = monsterData[attr];
max = cur;
break;
case 'CMD':
cur = parseInt(monsterData[attr]);
max = cur;
break;
case 'Acrobatics':
case 'Appraise':
case 'Bluff':
case 'Climb':
case 'Craft (any one)':
case 'Diplomacy':
case 'Disable Device':
case 'Disguise':
case 'Escape Artist':
case 'Fly':
case 'Handle Animals':
case 'Heal':
case 'Intimidate':
case 'Knowledge (religion)':
case 'Knowledge (planes)':
case 'Knowledge (history)':
case 'Knowledge (nature)':
case 'Knowledge (any one)':
case 'Linguistics':
case 'Perception':
case 'Ride':
case 'Sense Motive':
case 'Sleight of Hand':
case 'Spellcraft':
case 'Stealth':
case 'Survival':
case 'Swim':
case 'Use Magic Device':
re = RegExp(attr.replace('(', '\\(').replace(')', '\\)') + "\\s*(\\d*).*");
var match = re.exec(monsterData['Skills'])
if(match != null) {
cur = parseInt(monsterData['Skills'].match(re)[1]);
}
else {
cur = 0;
}
break;
default:
cur = parseInt(monsterData[attr]);
break;
}
log([attr, cur, max].join(':'))
if(cur != 0) {
createObj('attribute', {
characterid: character.id,
name: attr,
max: max,
current: cur
});
}
//*/
});
});
});
Basically
switch(attr) {
case 'AC':
//...
break;
case 'Str':
case 'Dex':
//...
break;
//...
}
will be
if(attr == 'AC') {
//...
} else if (attr == 'Str' || attr == 'Dex') {
//...
}
and so on. A case following another case directly will be converted to an or-expression.
BUT as the comments already point out, this is - with regard to readabilty - a bad idea...
You want to change this code section:
case 'Use Magic Device':
re = new RegExp(attr + "\\s*(\\d*).*");
cur = parseInt(monsterData['Skills'].match(re)[1]);
break;
to
case 'Use Magic Device':
re = RegExp(attr.replace('(', '\\(').replace(')', '\\)') + "\\s*(\\d*).*");
var match = re.exec(monsterData['Skills'])
if(match != null) {
cur = parseInt(match[1]);
} else {
cur = 0;
}
break;
This will check the match if it succeded (in this case the monster actually has the skill). If the match fails (the monst doesnt have this skill), then there won't be a parsing error and the break lets you hop out of the switch.
Further down you want to encase the call to createObject with an if:
if(cur != 0) {
createObj('attribute', {
characterid: character.id,
name: attr,
max: max,
current: cur
});
}
Furthermore, in your monsterAttributes there are two spelling errors which will cause cur to get NaN:
var monsterAttributes = [
/*...*/
'Disguise ', //there's a whitespace after Disguise, remove it!
/*...*/
'Use Magic device', //the 'd' has to be capital, as you check on 'Use Magic Device'
/*...*/
];
If I understood you correctly, you may simply check for attr being null and avoid the switch statement if this is the case. For example:
if( attr != undefined && attr != null ){
switch(attr){
//... the code as is in here
}// end switch
} // end if statement