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
- samuel henriques @samuel_henriques
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 - In reply toMitch_Willard⬆:samuel henriques @samuel_henriques
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();
Mitch Willard @Mitch_Willard
Amazing @samuel_henriques!
Both of these are great and worked like a charm!
thanks so much for your help.
Mitch