• Question about string.trim()

    From KenDB3@VERT/KD3NET to All on Thu Sep 8 10:11:46 2016
    Hi there folks,ããSo, I've been running the Solar Data app for a little while and have not runãinto this, but now that I put it out there for a wider audience, I seem to haveãencountered an issue. ããSomeone reported this error message to me:ãã9/7 11:14:09p Node 1 !javascript c:\sbbs\xtrn\solar\alco-solar.js line 275:ãtypeError: sfi.trim is not a functionããSo, my first question is, is the trim() function valid in JavaScript 1.8.5? I'mãguessing it is because it's been working in this app thus far, but maybe thisãis the first time something actually needed to be trimmed? ããWhat I find interesting is that the error flagged on a trim() on line 275, butãwas OK with the trim() on line 270: timestamp.trim(). ããThe code can be found here:ãhttps://github.com/KenDB3/alco-solar-JS/blob/master/alco-solar.jsããIf trim() is valid, then am I...ã1) Doing something wrong?ã2) Using it incorrectly?ã3) Not doing a good job of dealing with it if it is undefined? (lines 81-84)ã4) Gone plumb crazy (crazier than normal, I guess)?ããMy third question stood out to me because it is the only one that turns into anãInteger (0) if it is undefined, where the other ones turn into Strings ("NoãData"). ãã~KenDB3ãã---ã þ Synchronet þ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.usã
  • From echicken@VERT/ECBBS to KenDB3 on Thu Sep 8 11:39:34 2016
    9/7 11:14:09p Node 1 !javascript c:\sbbs\xtrn\solar\alco-solar.js line 275:ã> typeError: sfi.trim is not a functionãã> So, my first question is, is the trim() function valid in JavaScript 1.8.5?ã> I'm guessing it is because it's been working inããYes, it's a valid method on a String object.ã ã> What I find interesting is that the error flagged on a trim() on line 275,ã> but was OK with the trim() on line 270: timestamp.trim(). ããIn this case 'timestamp' is really just another String, since you're justãgrabbing the text from the feed and not turning it into a Number or Dateãobject, so that makes sense.ããMy best guess is that the 'solarflux' value was absent when this person fetchedãthe feed, or that they failed to fetch the feed altogether. On line 80, ifã'solardata.solarflux' is undefined, 'sfi' gets set to 0, a Number, which doesãnot have the '.trim()' method. Wrap that 0 in quotes on line 80 and thisãparticular problem should go away.ããAlso, if you wanted to, you could also shorten things a bit by doing somethingãlike:ããvar sfi = solardata.solarflux || '0';ãvar sfi_int = parseInt(sfi);ãvar ai = solardata.aindex || 'No Data';ã// etc.ããThat would be like a shorter version of:ããvar sfi = (typeof solardata.solarflux == 'undefined' ? '0' :ãsolardata.solarflux);ããWhich is a shorter version of:ããvar sfi = solardata.solarflux;ãif (sfi == undefined) {ã var sfi = '0';ã}ãã---ãechicken ãelectronic chicken bbs - bbs.electronicchicken.com - 416-273-7230ã þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.comã
  • From KenDB3@VERT/KD3NET to echicken on Thu Sep 8 12:32:12 2016
    Re: Question about string.trim()ã By: echicken to KenDB3 on Thu Sep 08 2016 11:39 amãã >> So, my first question is, is the trim() function valid in JavaScriptã >> 1.8.5? I'm guessing it is because it's been working inãã ec> Yes, it's a valid method on a String object.ããAwesome, thank you.ãã >> What I find interesting is that the error flagged on a trim() on lineã >> 275, but was OK with the trim() on line 270: timestamp.trim(). ãã ec> In this case 'timestamp' is really just another String, since you're justã ec> grabbing the text from the feed and not turning it into a Number or Dateã ec> object, so that makes sense.ãã ec> My best guess is that the 'solarflux' value was absent when this personã ec> fetched the feed, or that they failed to fetch the feed altogether. Onã ec> line 80, if 'solardata.solarflux' is undefined, 'sfi' gets set to 0, aã ec> Number, which does not have the '.trim()' method. Wrap that 0 in quotes onã ec> line 80 and this particular problem should go away.ãã ec> Also, if you wanted to, you could also shorten things a bit by doingã ec> something like:ãã ec> var sfi = solardata.solarflux || '0';ã ec> var sfi_int = parseInt(sfi);ã ec> var ai = solardata.aindex || 'No Data';ã ec> // etc.ãã ec> That would be like a shorter version of:ãã ec> var sfi = (typeof solardata.solarflux == 'undefined' ? '0' :ã ec> solardata.solarflux);ãã ec> Which is a shorter version of:ãã ec> var sfi = solardata.solarflux;ã ec> if (sfi == undefined) {ã ec> var sfi = '0';ã ec> }ããYou make it look so easy! Thanks man, that's incredibly helpful, I reallyãappreciate it. I'm learning lots playing with this stuff. I had a feeling, butãI wasn't sure. ããUpdates imminent :-)ãã~KenDB3ãã---ã þ Synchronet þ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.usã