lundi 13 avril 2020

How to simplify Nested Conditionals?

I think that using if / if else statement is too much code, I saw on some websites that are better to simplify nested conditions like some examples that shows on this website https://www.javascripttutorial.net/javascript-if-else/

I was thinking to get the wind direction names in an array or an object like this - let windNames = [Northerly','North Easterly','Easterly','South Easterly'...]

Can you guys help me out?

Many thanks.

function textDescription(d) {
    { //convert the wind direction as int to string
        if (d > 0 && d < 20) {
            return "Northerly";
        } else if (d > 30 && d < 60) {
            return "North easterly";
        } else if (d > 55 && d < 100) {
            return "Easterly";
        } else if (d > 110 && d < 140) {
            return "South easterly";
        } else if (d > 145 && d < 201) {
            return "Southerly";
        } else if (d > 201 && d < 215) {
            return "South westerly";
        } else if (d > 235 && d < 245) {
            return "Westerly";
        } else if (d > 225 && d < 325) {
            return "North westerly";
        } else if (d > 321) {
            return "Northerly";
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire