Kitz ADSL Broadband Information
adsl spacer  
Support this site
Home Broadband ISPs Tech Routers Wiki Forum
 
     
   Compare ISP   Rate your ISP
   Glossary   Glossary
 
Please login or register.

Login with username, password and session length
Advanced search  

News:

Author Topic: javascript coding  (Read 1401 times)

chenks

  • Kitizen
  • ****
  • Posts: 1106
javascript coding
« on: September 25, 2019, 09:43:05 AM »

any javascript experts here?
i've got a script that gets the numbers from a colum in a table, and then calculates the total and the average.
this works fine, but i want to add the % symbol at the end of it and can't work out the correct syntax (i know a javascript a bit to get by, but not enough).

Code: [Select]
$(function () {
    var BreakdownFTF = 0,
        col10= $("[id*=col10]").filter(function(){
            return parseFloat( $(this).text() ) !== 0
        })
    col10.each(function () {
        BreakdownFTF = BreakdownFTF + parseFloat($(this).html());
    });
    $("[id*=BreakdownFTF]").html(BreakdownFTF.toFixed(2));
    $("[id*=AvgBreakdownFTF]").html((BreakdownFTF/col10.length).toFixed(0));
});

the result of the script is displayed in the HTML using

Code: [Select]
<label id="AvgBreakdownFTF"/>
Logged

Weaver

  • Senior Kitizen
  • ******
  • Posts: 11459
  • Retd s/w dev; A&A; 4x7km ADSL2 lines; Firebrick
Re: javascript coding
« Reply #1 on: September 25, 2019, 12:15:36 PM »

I think you just add a % by doing
       existingvalue + '%'
So
    $("[id*=AvgBreakdownFTF]").html((BreakdownFTF/col10.length).toFixed(0) + '%');
});
« Last Edit: September 25, 2019, 12:23:21 PM by Weaver »
Logged

chenks

  • Kitizen
  • ****
  • Posts: 1106
Re: javascript coding
« Reply #2 on: September 25, 2019, 01:16:58 PM »

yeah that's what i though too.
but that didn't seem to display the symbol, it's also didn't error either.

edit - actually maybe i had a typo the first time, as that is now working correctly.
« Last Edit: September 25, 2019, 01:19:01 PM by chenks »
Logged

Alex Atkin UK

  • Addicted Kitizen
  • *****
  • Posts: 5282
    • Thinkbroadband Quality Monitors
Re: javascript coding
« Reply #3 on: September 26, 2019, 11:48:21 PM »

Its easy to trip up when things are encased in multiple brackets.

I sometimes spend hours trying to figure out why something isn't working just to find out its the wrong side of a curly bracket, or one is missing.
Logged
Broadband: Zen Full Fibre 900 + Three 5G Routers: pfSense (Intel N100) + Huawei CPE Pro 2 H122-373 WiFi: Zyxel NWA210AX
Switches: Netgear MS510TXUP, Netgear MS510TXPP, Netgear GS110EMX My Broadband History & Ping Monitors

sevenlayermuddle

  • Helpful
  • Addicted Kitizen
  • *
  • Posts: 5369
Re: javascript coding
« Reply #4 on: September 27, 2019, 12:55:53 AM »

I have spent most of my working life, and my retirement life so far, writing software.

Once, I was envious of those who unswervingly knew the rules of operator precedence.   I even tried to learn these rules myself.   Then I figured out... why bother?   Just add brackets, most languages allow them.

Same goes for curly brackets, to create scope of code.  Often it is implicit, so the brackets can be omitted.  Until of course, somebody later adds another statement which alters code flow, rendering the brackets necessary after all. So, my view, use them, even if not needed.

I am sure somebody will be along soon, probably a cousin of the people with whom I worked that always knew order of precedence.   They will point out that, in above, I have referred to braces and parenthesis as if they were brackets. :D
Logged

burakkucat

  • Respected
  • Senior Kitizen
  • *
  • Posts: 38300
  • Over the Rainbow Bridge
    • The ELRepo Project
Re: javascript coding
« Reply #5 on: September 27, 2019, 02:20:08 AM »

I am sure somebody will be along soon, . . .  They will point out that, in above, I have referred to braces and parenthesis as if they were brackets. :D

The pedantic cat silently passes by . . .  ;)
Logged
:cat:  100% Linux and, previously, Unix. Co-founder of the ELRepo Project.

Please consider making a donation to support the running of this site.