0

I've got a question over here..

I got a really simple BMI calculator, its not even a calculator but the think about the idea behind it.. I just made a Case but it didn't work. (I know the value's to be healthy or obesitas are wrong, just doing it for the idea!)

Here You can see the code where I got the problem(Line 19 at the case with <=)

<html>
<head>
    <title>Test BMI</title>
    <style>
        label{
        display:block;
        }
    </style>
    <script>
        function calc(){

            var lengte = document.getElementById("length").value;
            var gewicht = document.getElementById("gewicht").value;
            var bmi = gewicht / (lengte * lengte);
            var getal = parseInt(bmi);
            document.getElementById('uitkomst').innerHTML += "";
            switch (getal){

            case <= 17:
            document.getElementById('uitkomst').innerHTML = "U are far to light " + gewicht + "kilo!";
            break;

            case <= 23 :

            document.getElementById('uitkomst').innerHTML = "U are in good shape" + gewicht + "kilo!"; 
            break;

            default:
            document.getElementById('uitkomst').innerHTML = "just a test";




            }
            document.getElementById('advies').innerHTML = bmi.toFixed(1);
        }
    </script>
</head>
<body>
    <div id="wrapper" style="margin-left:50px;">
        <h1>BMI Berekenen</h1>
        <label>Lengte CM</label>
            <input type="text" id="length"><br/>

        <label>Gewicht KG</label>
            <input type="text" id="gewicht">
        <button id="bereken" onclick="calc()">Bereken je BMI</button>
        <div id="uitkomst">
        </div>
        <div id="advies">
        </div>
    </div>
</body>
</html>

Can anybody help me out with this little problem I got in my code:)

Thanks in Advance!

Taykklan
  • 45
  • 6

3 Answers3

4

You can not have conditions like this in case. You should use if-else if-else blocks like bellow.

var getal = parseInt(bmi);
document.getElementById('uitkomst').innerHTML += "";
if(getal <= 17){
    document.getElementById('uitkomst').innerHTML = "U are far to light " + gewicht + "kilo!";
}
else if(getal <= 23){
    document.getElementById('uitkomst').innerHTML = "U are in good shape" + gewicht + "kilo!"; 
}
else
    document.getElementById('uitkomst').innerHTML = "just a test";
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • Ah just remembered that cases are not for that kind of work:) Thanks for remembering me! Thanks for the help! – Taykklan Dec 31 '14 at 16:53
  • Why *should* you use if-else statements? There are many other valid, if not better, workarounds. – theonlygusti Dec 31 '14 at 16:54
  • 1
    @theonlygusti Like what workarounds? If-else statements are easy to read, intuitive, and efficient for what he's doing. – mbomb007 Dec 31 '14 at 17:17
1

You can only have values on the case conditions unless you use the 'method 2' on the bottom of this link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

Method two

This is the "hacky" method; on line 2, where you would usually find switch(foo), we instead put an invariant true, and place conditional logic (instead of constants) into the case statements:

var foo = 1;
switch (true) { // invariant TRUE instead of variable foo
    case foo >= 0 && foo <= 3:
        alert('yes');
        break;
    default:
        alert('not');
}
jorge.alonso
  • 289
  • 2
  • 5
  • Link only answers are considered not useful. Links break, and a phrase like "'method 2' on the bottom of this link" will then be useless. Please include the relevant parts in your answer, and keep the link only for reference or attribution. – GolezTrol Dec 31 '14 at 17:03
0

In switch statements, you can't have comparative operators, like <=, so

case <= 17:

Simply won't work. (reference)

The problem you are now faced with is to devise an alternate method, and that problem has been answered before on the site here.

My personal favourite work-around proposed on that page is this one:

switch (true){

  case getal <= 17:
    //....

called a switch-range, because it involves the least typing, unlike an if-else if-else monstrosity.

Community
  • 1
  • 1
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • Least typing? You forgot you need to add `break` at each branch? Also, this is slower, according to the answer you linked. – GolezTrol Dec 31 '14 at 17:01