No internet connection
  1. Home
  2. How to

Issue with >= value.

By Mitch Willard @Mitch_Willard
    2022-10-06 05:44:38.903Z

    Hi all,

    I'm having a strange issue with a script of mine. The idea is base on the import of the let numberOfMixes = prompt('How Many Spots are there?') it will cycle through and only do the commands up to the value enter into the prompt
    It works perfectly, However the the issue I'm running into is when a value is entered under 10, eg "3", it with do only the functions for 3 and below, but then will do all values 10 and above even though they should be excluded with >= .

    Am I approaching this the wrong way?

    Here below is a simple example of the code that produces the same issue.

    
    let numberOfMixes = prompt('How Many Spots are there?')
    
    
    if (numberOfMixes >= '1') {
        sf.interaction.displayDialog({ prompt: 'Mix 1', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '2') {
        sf.interaction.displayDialog({ prompt: 'Mix 2', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '3') {
        sf.interaction.displayDialog({ prompt: 'Mix 3', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '4') {
        sf.interaction.displayDialog({ prompt: 'Mix 4', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '5') {
        sf.interaction.displayDialog({ prompt: 'Mix 5', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '6') {
        sf.interaction.displayDialog({ prompt: 'Mix 6', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '7') {
        sf.interaction.displayDialog({ prompt: 'Mix 7', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '8') {
        sf.interaction.displayDialog({ prompt: 'Mix 8', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '9') {
        sf.interaction.displayDialog({ prompt: 'Mix 9', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '10') {
        sf.interaction.displayDialog({ prompt: 'Mix 10', giveUpAfterSeconds: 2 });
    }
    if (numberOfMixes >= '11') {
        sf.interaction.displayDialog({ prompt: 'Mix 11', giveUpAfterSeconds: 2 });
    }
    
    

    thanks

    Mitch

    Solved in post #2, click to view
    • 3 replies
    1. samuel henriques @samuel_henriques
        2022-10-06 09:15:04.114Z2022-10-06 09:34:41.594Z

        Hello Mitch,
        I've had a similar problem in the past.

        The thing is more/less is meant to be used in numbers, not strings. And when javaScript "forcefully" converts a string to number with two numbers (ex:11), for him, this is 1. Thats why 10 and 11 on your script are 1 and 1.

        This is how I changed the code to fix it:

        if (+numberOfMixes >= 1) {
            sf.interaction.displayDialog({ prompt: 'Mix 1', giveUpAfterSeconds: 2 });
        }
        

        This was my lesson,
        sometimes you need to convert strings to a number

        ReplySolution
        1. In reply toMitch_Willard:
          samuel henriques @samuel_henriques
            2022-10-06 09:49:46.461Z2022-10-06 10:51:16.841Z

            And you can simplify you code;

            function displayMix(value) {
                sf.interaction.displayDialog({ prompt: value, giveUpAfterSeconds: 2 });
            };
            
            function displayNumberOfMixes() {
            
                let numberOfMixes = prompt('How Many Spots are there?')
            
                if (Number.isNaN(+numberOfMixes) && numberOfMixes !== undefined) { displayMix("Numbers only please!"); displayNumberOfMixes() }
            
                for (let i = 0; i < +numberOfMixes; i++) {
                    displayMix(`Mix ${i + 1}`)
                };
            };
            
            displayNumberOfMixes();
            
            1. Mitch Willard @Mitch_Willard
                2022-10-06 11:04:54.532Z

                Amazing @samuel_henriques!

                Both of these are great and worked like a charm!

                thanks so much for your help.

                Mitch