Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Compare problem

edited October 2016 in Help! with 2.0
My test code is:
{(set: $target="12")
(set: $current="$num1"+"$num2")
(set: $num1 = "1")
(set: $num2 = "2")
(set: $test = "12")

$target = $current?
(if: $target is $current)[
YES]
(else:)[NO]
<br>

$target = $test?
(if: $target is $test)[
YES]
(else:)[NO]
}

Result:
12 = 12 ? NO
12 = 12 ? YES

Debug says:
12 $target = 1 $num1 2 $num2 $current
12 $target = 12 $test

soooo... Problem is obvious. But how can I solve it?

Comments

  • Well... anyone? :smile:
  • edited October 2016
    It's not clear whether your intention was to use actual numbers or numeric strings here. If it was the former, then you need to drop the quotes.

    Regardless, your primary issue is with your assignment to $current. You're assigning it the value of the expression "$num1"+"$num2", which becomes the string "$num1$num2"—using Test mode should show this. That's why it doesn't equal $target when you compare them—i.e. "12" does not equal "$num1$num2", even though they print out similarly. Basically, you assigned it the names of the variables, $num1 and $num2, rather than their values. Whether your intention was to use numbers or numeric strings, the assignment should have been something like:
    (set: $current = $num1 + $num2)
    

    Also, if your intention was to assign $current the values of $num1 and $num2, rather than their names, then you cannot use them in an expression before you've actually done their assignment first—i.e. you have them out of order.

    Your assignments should look something like the following if you wanted numeric strings:
    (set: $target = "12")
    (set: $num1 = "1")
    (set: $num2 = "2")
    (set: $current = $num1 + $num2)
    (set: $test = "12")
    

    Or the following if you wanted actual numbers:
    (set: $target = 12)
    (set: $num1 = 10)
    (set: $num2 = 2)
    (set: $current = $num1 + $num2)
    (set: $test = 12)
    

    The comparisons don't need to change either way.
  • edited October 2016
    (set: $target="1a")
    (set: $var1 = "1")
    (set: $var2 = "a")
    (set: $current = $var1+$var2)
    

    Yes, without quotes in $current it works perfectly!
    Thanks a lot!
  • UPD: If you need to use random numbers in the same situation - this code will work:
    (set: $var1=String((random: 0, 9)))
    

    if you try to use quotes instead of String() - the variable will get the value "(random: 0, 9)" instead of the random function's result. So it will not be able to compare too.

    (which is a big surprise for me cause I thought that String() and quotes are the same thing)

    I hope it will be useful.
  • or you could use Harlowe's (text: ) macro to convert the number to a String instead of Javascript String object's constructor.
Sign In or Register to comment.