PricingBlog

How to build a formula to translate a given value (multiple options) to a specific text?

  • awwwwwake-1226811409187471360

    awwwwwake

    1 year ago

    I want to use the weather api that when given for example the wind speed instead of the speed, display a custom message. I understand i have to use the IF block but i'm having a hard time wrapping my head around what is the structure i should follow.

    After having that formula, I will hook it up with the current wind value in the front and just add the formula to "translate" the value to text.


    function wind(speed) {
    if (speed < 0.2) {
    return {
    description: "Calm",
    recommendation: "Enjoy the quiet weather; it's like nature's lullaby."
    };
    } else if (speed >= 0.3 && speed <= 1.5) {
    return {
    description: "Light air",
    recommendation: "Perfect breeze for a leisurely stroll or bike ride."
    };
    } else if (speed >= 1.6 && speed <= 3.3) {
    return {
    description: "Light breeze",
    recommendation: "It's breezy but not too strong; perfect for outdoor activities. Secure your hat or scarf if you're out."
    };
    } else if (speed >= 3.4 && speed <= 5.4) {
    return {
    description: "Gentle breeze",
    recommendation: "A gentle breeze is blowing; make sure your hat stays put!"
    };
    } else if (speed >= 5.5 && speed <= 7.9) {
    return {
    description: "Moderate breeze",
    recommendation: "Hold onto your hats, folks! The wind is picking up a bit."
    };
    } else if (speed >= 8.0 && speed <= 10.7) {
    return {
    description: "Fresh breeze",
    recommendation: "A fresh breeze is blowing; bring a light jacket just in case."
    };
    } else if (speed >= 10.8 && speed <= 13.8) {
    return {
    description: "Strong breeze",
    recommendation: "Hang onto your hats! It's getting windy out there. Secure any loose items and maybe choose a hairstyle that can handle the wind."
    };
    ……
    1
  • Tod-1226811410680512543

    Tod

    1 year ago

    Great energy @awwwwwake! Your continuous contribution to the toddle Community just made you advance to Community Level 1!
  • awwwwwake-1226811859370377247

    awwwwwake

    1 year ago

    How to build a formula to translate a given value (multiple options) to a specific text?
  • yoelfdz-1226812887570452491

    yoelfdz

    1 year ago

    You should be able to do that with that if condition. Make sure that you are inputing numbers and not strings
  • awwwwwake-1226858914453389363

    awwwwwake

    1 year ago

    thanks! i found out a way!
    1226858914130169907-Screenshot_2024-04-08_at_14.39.13.jpg
    ❤️1
  • lululucaschae-1227191342652723232

    lululucaschae

    1 year ago

    I wonder if a swtich-case node is in development or being planned. I had 10+ cases and ended up just creating a custom formula instead 🥲
  • andreasmoller-1227223586763440189

    Andreas Møller

    1 year ago

    It is. The If formula in toddle is actually implemented as a switch / case. I just couldn't find a good way to represent it visually in the formula editor.
  • It quickly became very difficult to process visually :/
    ❤️1
  • lululucaschae-1227224974905839657

    lululucaschae

    1 year ago

    I see! For a second I thought something like the Object node panel can be used, where "case" goes into the place of "Key" and the return statements go in the place of "Value" (where you can also plug in anything), but ofc there would be a lot of edge cases to think about 🙂
  • andreasmoller-1227231442526797916

    Andreas Møller

    1 year ago

    The main one is that each case has both a condition and a value, which makes it explode in size 😦
  • But it is very solvable, someone just need to spend a bit more time on it