If statement not being entered in for loop

154 views Asked by At

I have a geojson file with earthquake point features that need to be styled based on the magnitude of the quake. The switch statement works correctly but it's a lot of code that I think could be done neater:

switch (feature.properties.mag){
            case 1.0 : return layer.setRadius(5);
            case 1.1 : return layer.setRadius(5);
            case 1.2 : return layer.setRadius(5);
            case 1.3 : return layer.setRadius(5);
            case 1.4 : return layer.setRadius(5);
            case 1.5 : return layer.setRadius(5);
            case 1.6 : return layer.setRadius(5);
            case 1.7 : return layer.setRadius(5);
            case 1.8 : return layer.setRadius(5);
            case 1.9 : return layer.setRadius(5);
            case 2.0 : return layer.setRadius(8);
            case 2.1 : return layer.setRadius(8);
            case 2.2 : return layer.setRadius(8);
            case 2.3 : return layer.setRadius(8);
            case 2.4 : return layer.setRadius(8);
            case 2.5 : return layer.setRadius(8);
            case 2.6 : return layer.setRadius(8);
            case 2.7 : return layer.setRadius(8);
            case 2.8 : return layer.setRadius(8);
            case 2.9 : return layer.setRadius(8);
            case 3.0 : return layer.setRadius(10);
            case 3.1 : return layer.setRadius(10);
            case 3.2 : return layer.setRadius(10);
            case 3.3 : return layer.setRadius(10);
            case 3.4 : return layer.setRadius(10);
            case 3.5 : return layer.setRadius(10);
            case 3.6 : return layer.setRadius(10);
            case 3.7 : return layer.setRadius(10);
            case 3.8 : return layer.setRadius(10);
            case 3.9 : return layer.setRadius(10);
            case 4.0 : return layer.setRadius(15); 
            case 4.1 : return layer.setRadius(15);
            case 4.2 : return layer.setRadius(15);
            case 4.3 : return layer.setRadius(15);
            case 4.4 : return layer.setRadius(15);
            case 4.5 : return layer.setRadius(15);
            case 4.6 : return layer.setRadius(15);
            case 4.7 : return layer.setRadius(15);
            case 4.8 : return layer.setRadius(15);
            case 4.9 : return layer.setRadius(15);
            case 5.0 : return layer.setRadius(20);
            default: layer.setRadius(8);
        }

I think the for loop might be caught up on the conditional statement because all the points are styled the same and not with the else value:

for (var i=0; i<feature.properties.length; i++){
            if (1.0 <= feature.properties.mag < 2.0){
                 return layer.setRadius(5);
            }

            else if (2.0 <= feature.properties.mag < 3.0){
                 return layer.setRadius(8);
            }

            else if (3.0 <= feature.properties.mag < 4.0){
                 return layer.setRadius(10);
            }

            else if (4.0 <= feature.properties.mag < 5.0){
                 return layer.setRadius(15);
            }

            else{
                 return layer.setRadius(2);
            }
            }
2

There are 2 answers

4
Wee Tai On BEST ANSWER

First, your conditionals are not right. Try them like this setting a lower and upper bound.

if (feature.properties.mag >= 1.0 && feature.properties.mag < 2.0){
    return layer.setRadius(5);
}
// the rest similar to the above

Unless there is more to feature.properties we aren't seeing, the for-loop is unnecessary.

3
Kudin On

Why not use Math.floor() on feature.properties.mag?

switch (Math.floor(feature.properties.mag)){
       case 1.0 : return layer.setRadius(5);
       case 3.0 : return layer.setRadius(10);
       case 4.0 : return layer.setRadius(15); 
       case 5.0 : return layer.setRadius(20);
       default: return layer.setRadius(8);
}

Switch is a replacement for long if-elseif chains