Welcome to the Cumulus Support forum.

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Cumulus MX V4 beta test release 4.0.0 (build 4017) - 17 March 2024

Legacy Cumulus 1 release v1.9.4 (build 1099) - 28 November 2014 (a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

Air quality JavaScript

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
User avatar
dazza1223
Posts: 860
Joined: Sun 25 Jan 2015 8:41 pm
Weather Station: Davis Vantage Pro 2 plus
Operating System: Raspberry pi 4 (4gb)
Location: Worthing
Contact:

Air quality JavaScript

Post by dazza1223 »

Hi guys I'm playing with a bit of JavaScript I'm trying to achieve that when the air quality goes to a specific level it will try to out put a text to Low High or medium or extreme

but when i set the numbers to the one below it ment to output GOOD but all i get is nothing i cant to to get my head around this any help plz


var aqi = rawdataextras[56];
if (Aqi=="1.0"){var air="Good";} else
if (Aqi=="1.1"){var air="Good";} else
if (Aqi=="1.2"){var air="Good";} else
if (Aqi=="1.3"){var air="Good";} else
if (Aqi=="1.4"){var air="Good";} else
if (Aqi=="1.5"){var air="Good";} else
if (Aqi=="1.6"){var air="Good";} else
if (Aqi=="1.7"){var air="Good";} else
if (Aqi=="1.8"){var air="Good";} else
if (Aqi=="1.9"){var air="Good";} else
{ var air="";}
}
else
{
$("#airquality").show();
}
$("#airquality").html("" + air);
Have fun and keep learning

dazza :D

https://www.davisworthing.co.uk

Image
freddie
Posts: 2433
Joined: Wed 08 Jun 2011 11:19 am
Weather Station: Davis Vantage Pro 2 + Ecowitt
Operating System: GNU/Linux Ubuntu 22.04 LXC
Location: Alcaston, Shropshire, UK
Contact:

Re: Air quality JavaScript

Post by freddie »

Not 100% sure with JavaScript but it looks as if you are declaring the variable in the if/else blocks, so it will be out of scope by the time you use it. I think you need to declare it before your if/else and just set it inside the if/else block.
Freddie
Image
User avatar
HansR
Posts: 5870
Joined: Sat 20 Oct 2012 6:53 am
Weather Station: GW1100 (WS80/WH40)
Operating System: Raspberry OS/Bullseye
Location: Wagenborgen (NL)
Contact:

Re: Air quality JavaScript

Post by HansR »

freddie wrote: Sun 16 Jan 2022 7:23 am Not 100% sure with JavaScript but it looks as if you are declaring the variable in the if/else blocks, so it will be out of scope by the time you use it. I think you need to declare it before your if/else and just set it inside the if/else block.
@freddie, @dazza:
hmmm... Though not as it probably would have been taught at school (just declare it once) I don't think the declaration is the point here (var makes it global, let would have made it a local variable)

Apparently at the start - which is not shown here - the $("#airquality") is hidden because why else would you do a

Code: Select all

$("#airquality").show();
So, no matter what you do to the variable air and no matter how many times you assign it to, $("#airquality") will not show any value on your page.

So the big question is: is it truly hidden at the start.

But to be honest: @dazza: go follow some (online) programming course. Many good (online) courses exist. I don't think the Cumulus forum is created with the purpose of being an education in JavaScript in mind and we are not equipped or educated as teachers (well some may be accidentally but you get it I hope).
Hans

https://meteo-wagenborgen.nl
CMX build 4017+ ● RPi 3B+ ● Raspbian Linux 6.1.21-v7+ armv7l ● dotnet 8.0.3
freddie
Posts: 2433
Joined: Wed 08 Jun 2011 11:19 am
Weather Station: Davis Vantage Pro 2 + Ecowitt
Operating System: GNU/Linux Ubuntu 22.04 LXC
Location: Alcaston, Shropshire, UK
Contact:

Re: Air quality JavaScript

Post by freddie »

HansR wrote: Sun 16 Jan 2022 10:58 am
freddie wrote: Sun 16 Jan 2022 7:23 am Not 100% sure with JavaScript but it looks as if you are declaring the variable in the if/else blocks, so it will be out of scope by the time you use it. I think you need to declare it before your if/else and just set it inside the if/else block.
var makes it global
That's what I meant about not being 100% sure - I wasn't aware of this (terrible, IMHO) feature of JavaScript.
Freddie
Image
User avatar
dazza1223
Posts: 860
Joined: Sun 25 Jan 2015 8:41 pm
Weather Station: Davis Vantage Pro 2 plus
Operating System: Raspberry pi 4 (4gb)
Location: Worthing
Contact:

Re: Air quality JavaScript

Post by dazza1223 »

Ok thank for the tips but I've sorted it out now just when back to it after a good night sleep then found out where I went wrong in the code meny thanks
Have fun and keep learning

dazza :D

https://www.davisworthing.co.uk

Image
User avatar
mcrossley
Posts: 12685
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: Air quality JavaScript

Post by mcrossley »

My understanding...

var scopes to the enclosing function - or global if outside a function.
let scopes to the enclosing block - normally in an if, while, switch etc. - or the function if not in a block. If outside a function then the scope is the script.

vars are also 'hoisted', in that their declaration is moved to the top of the function scope at runtime. lets are not, they are initialised in-place.
User avatar
HansR
Posts: 5870
Joined: Sat 20 Oct 2012 6:53 am
Weather Station: GW1100 (WS80/WH40)
Operating System: Raspberry OS/Bullseye
Location: Wagenborgen (NL)
Contact:

Re: Air quality JavaScript

Post by HansR »

@mcrossley: yes, I was too short. I should have referred to the spec :|
Hans

https://meteo-wagenborgen.nl
CMX build 4017+ ● RPi 3B+ ● Raspbian Linux 6.1.21-v7+ armv7l ● dotnet 8.0.3
Post Reply