Home » Javascript » Javascript operators in switch case

Javascript operators in switch case

Posted by: admin August 19, 2018 Leave a comment

Questions:

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.

example stats from the UI

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';
                    }
Answers: