View Single Post
Posts: 339 | Thanked: 1,623 times | Joined on Oct 2013 @ France
#17
I am not sure I understand.
Originally Posted by Markkyboy View Post
I have used different variations with success, here's one example using a single '=' operator with success.
While the previous message you said it didn't work:
Originally Posted by Markkyboy View Post
Problem is, my output text is stuck on "NE" and does not change when selecting different days, so clearly there is something wrong with my code, but I have no idea what, so any clues here would be appreciated.
So what is this success you are talking about ?

From what I understand (I am more of a C developer than JS...):
First test : the test is using the assignment operator and not the comparison one, so you are actually setting the variable weather.windDirection to 0.
I assume in JS its behavior is like in C, where the whole instruction returns the value that was assigned, so "if(weather.windDirection=0)" is then evaluated to " if(0)" which is false.
So it takes the first else. There the variable is assigned to 45, and likewise the tests is "if(45)" which is true (not 0), so it take the instruction windDirectionText='NE', which probably return the result of this assignement as the return value of the block, so it is always showing "NE", no matter the variable weather.windDirection which gets overwritten constantly by each "test".

I could be wrong in this analysis, but something like the following should be better:
Code:
DetailItem {
            //% "WindDirection"
            label: qsTrId("Wind direction")
            value: {
                if      (weather.windDirection == 0) return 'N';
                else if (weather.windDirection == 45) return 'NE';
                else if (weather.windDirection == 90) return 'E';
                else if (weather.windDirection == 135) return 'SE';
                else if (weather.windDirection == 180) return 'S';
                else if (weather.windDirection == 225) return 'SW';
                else if (weather.windDirection == 270) return 'W';
                else if (weather.windDirection == 315) return 'NW';
            }
        }
I am not sure of the need of the "return" here, I usually use ternary operator for this kind of tests, which gets ugly fast.

Are the only possible values those 45 degrees multiples ? If not, you should use "<=" comparison for it to work for other values (and adding 22.5 degrees to each value).
 

The Following 3 Users Say Thank You to Zeta For This Useful Post: