Need new variable based on 2 existing variables |
Need new variable based on 2 existing variables |
Cartoony |
May 22 2024, 01:47 AM
Post
#1
|
Newbie Group: Members Posts: 12 Joined: 27-September 23 Member No.: 29,062 |
Here's my code for a cricket spectator scoreboard, which pulls its data from a json file. (Someone else wrote the original code, I just added bits to it)
The json file includes various score data, specifically 'total' & 'LastWicket' However I want to display the current partnership which would be total - LastWicket Could someone tell me what I need to add & where please? CODE <html> <head> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> <meta content="utf-8" http-equiv="encoding"> <link rel="stylesheet" type="text/css" href="./css/spectatornoborders.css"> <script src="./jquery/jquery-3.5.1.min.js"></script> <!--<script src="./jquery/jquery.mobile-1.3.0.min.js"></script> <script src="./jquery/mobiscroll.custom-2.4.4.min.js"></script> --> <script type="text/javascript"> $(function() { getStatus(); }); function getStatus() { $fileName = "scorePCS.json?date=" + new Date().toLocaleString(); //ask for the scorePCS.json file generated by updateSpectator.php. Include the date to stop the browser caching an old copy $.getJSON($fileName, function(data) { document.getElementById("BatTeamName").innerHTML=formatdigit2s(data.BatTeamName); document.getElementById("total").innerHTML=formatdigit2s(data.total); document.getElementById("wickets").innerHTML=formatdigit2s(data.wickets); document.getElementById("overs").innerHTML=formatdigit2s(data.overs); // document.getElementById("OversBowled").innerHTML=formatdigit2s(data.OversBowled); document.getElementById("OversRem").innerHTML=formatdigit2s(data.OversRem); //Placeholder for Target - dealt with later document.getElementById("RunsReq").innerHTML=formatdigit2s(data.RunsReq); document.getElementById("ReqRunRate").innerHTML=formatdigit2s(data.ReqRunRate); document.getElementById("LastWicket").innerHTML=formatdigit2s(data.LastWicket); //PSHIP needs to be a calculation BTS-LWK document.getElementById("DLSTarget").innerHTML=formatdigit2s(data.DLSTarget); document.getElementById("DLSPar").innerHTML=formatdigit2s(data.DLSPar); document.getElementById("BatAname").innerHTML=formatdigit2s(data.Bat1Name); document.getElementById("BatAscore").innerHTML=formatdigit2s(data.BatAscore); document.getElementById("BatABallsFaced").innerHTML=formatdigit2s(data.BatABallsFaced); document.getElementById("BatAonStrike").innerHTML=formatdigit2s(data.Bat1onStrike); document.getElementById("BatBname").innerHTML=formatdigit2s(data.Bat2Name); document.getElementById("BatBscore").innerHTML=formatdigit2s(data.BatBscore); document.getElementById("BatBBallsFaced").innerHTML=formatdigit2s(data.BatBBallsFaced); document.getElementById("BatBonStrike").innerHTML=formatdigit2s(data.Bat2onStrike); document.getElementById("FieldTeamName").innerHTML=formatdigit2s(data.FieldTeamName); document.getElementById("CurrBowlName").innerHTML=formatdigit2s(data.CurrBowlName); document.getElementById("CurrBowlFigs").innerHTML=formatdigit2s(data.CurrBowlFigs); document.getElementById("PrevBowlName").innerHTML=formatdigit2s(data.PrevBowlName); document.getElementById("PrevBowlFigs").innerHTML=formatdigit2s(data.PrevBowlFigs); document.getElementById("CurrentOver").innerHTML=formatdigit2s(data.CurrentOver); { // document.getElementById("target2Heading").className = "noborder"; // document.getElementById("target2Heading").innerHTML=""; // document.getElementById("target2").className = "noborder"; // document.getElementById("target2").innerHTML=""; document.getElementById("target1").innerHTML=formatdigit2s(data.target); // document.getElementById("dl").innerHTML=formatdigit2s(data.dltarget); } }); setTimeout("getStatus()",10000); //rerun the function every 10 seconds (including loading the score.json file) } function formatdigit2s(digit2s) { var returnStr=""; //split the value into its digit2 parts, as we want to strip the dashes digit2Array=digit2s.split(""); for (i=0; i<digit2Array.length; i++) { //ignore this digit2 if it is a dash if (digit2Array[i] != "-") { returnStr=returnStr.concat(digit2Array[i]); } } // if the input was all dashes, then lets return a 0 if (returnStr==""){ returnStr="0"; } return returnStr; } </script> </head> <body> <center> <table width="95%" id="topRow"> <tr class="rowpad"> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td colspan="2" align="center" valign="middle" class="digit2" id=BatTeamName>-</td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> </tr> <tr class="rowpad"> <td align="center" valign="middle">Total</td> <td align="center" valign="middle">Wickets</td> <td align="center" valign="middle">Overs</td> <td align="center" valign="middle">OVRem</td> <td align="center" valign="middle">Target</td> <td align="center" valign="middle">Runs Req</td> </tr> <tr class="rowpad"> <td align="center" valign="middle" class="digit2" id=total>-</td> <td align="center" valign="middle" class="digit2" id=wickets>-</td> // <td align="center" valign="middle" class="digit2" id=overs>-</td> <td align="center" valign="middle" class="digit2" id=OversRem>-</td> <td align="center" valign="middle" class="digit2" id=target1>-</td> <td align="center" valign="middle" class="digit2" id=RunsReq>-</td> </tr> // <tr class="rowpad"> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> </tr> <tr class="rowpad"> <td align="center" valign="middle"> </td> <td align="center" valign="middle">Req Rate</td> <td align="center" valign="middle">Last Wkt</td> <td align="center" valign="middle">Pship</td> <td align="center" valign="middle">DL Tgt</td> <td align="center" valign="middle">DL Par</td> </tr> <tr class="rowpad"> <td align="center" valign="middle"> </td> <td align="center" valign="middle" class="digit2" id=ReqRunRate>-</td> <td align="center" valign="middle" class="digit2" id=LastWicket>-</td> //lastwkt <td align="center" valign="middle" class="digit2" id=pship>-</td> <td align="center" valign="middle" class="digit2" id=DLSTarget>-</td> //DLTgt <td align="center" valign="middle" class="digit2" id=DLSPar>-</td> //DLPar </tr> <tr class="rowpad"> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> </tr> <tr class="rowpad"> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle">Score</td> <td align="center" valign="middle">Balls</td> <td align="center" valign="middle">On Str</td> </tr> <tr class="rowpad"> <td colspan="3" align="right" valign="middle" class="digit2" id=BatAname>-</td> <td align="center" valign="middle" class="digit2" id=BatAscore>-</td> <td align="center" valign="middle" class="digit2" id=BatABallsFaced>-</td> <td align="center" valign="middle" class="digit2" id=BatAonStrike>-</td> </tr> <tr class="rowpad"> <td colspan="3" align="right" valign="middle" class="digit2" id=BatBname>-</td> <td align="center" valign="middle" class="digit2" id=BatBscore>-</td> <td align="center" valign="middle" class="digit2" id=BatBBallsFaced>-</td> <td align="center" valign="middle" class="digit2" id=BatBonStrike>-</td> </tr> <tr class="rowpad"> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> </tr> <tr class="rowpad"> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> <td colspan="2" align="center" valign="middle" class="digit2" id=FieldTeamName>-</td> <td align="center" valign="middle"> </td> <td align="center" valign="middle"> </td> </tr> <tr class="rowpad"> <td colspan="3" align="right" valign="middle" class="digit2" id=CurrBowlName>-</td> <td align="center" valign="middle" class="digit2" id=CurrBowlFigs>-</td> <td align="center" valign="middle"> </td> <td align="center" valign="middle">Current Over</td> </tr> <tr class="rowpad"> <td colspan="3" align="middle" valign="middle" class="digit2" id=PrevBowlName>-</td> <td align="center" valign="middle" class="digit2" id=PrevBowlFigs>-</td> <td align="center" valign="middle"> </td> <td align="center" valign="middle" class="digit2" id=CurrentOver>-</td> </tr> </table> </center> </body> </html> This post has been edited by Cartoony: May 22 2024, 02:39 AM |
Christian J |
May 22 2024, 05:37 PM
Post
#2
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
The json file includes various score data, specifically 'total' & 'LastWicket' However I want to display the current partnership which would be total - LastWicket I didn't understand that, could you elaborate? Is it a hyphen or minus sign in "total - LastWicket"? |
Cartoony |
May 23 2024, 01:53 AM
Post
#3
|
Newbie Group: Members Posts: 12 Joined: 27-September 23 Member No.: 29,062 |
The json file includes various score data, specifically 'total' & 'LastWicket' However I want to display the current partnership which would be total - LastWicket I didn't understand that, could you elaborate? Is it a hyphen or minus sign in "total - LastWicket"? The json file contains variables called total & LastWkt. Total being the current score & lastwkt being the score when the last person was out. But the app doesn't send a variable for partnership, so I was hoping to calculate the partnership from within the html. The partnership would be the current total minus the last wicket eg Total 150 LastWkt 115 Partnership 35 i.e. pship = total - lastwkt |
Christian J |
May 23 2024, 05:46 AM
Post
#4
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
That sounds easy if the JSON values of total and LastWkt are numbers. But in https://forums.htmlhelp.com/index.php?showt...c=61470&hl= you mentioned that the JSON values could be dashes as well. What do you want the script to output in that case?
|
coothead |
May 23 2024, 08:11 AM
Post
#5
|
Advanced Member Group: Members Posts: 226 Joined: 12-January 23 From: chertsey, a small town 25 miles south west of london, england Member No.: 28,743 |
coothead |
Cartoony |
May 24 2024, 06:30 AM
Post
#6
|
Newbie Group: Members Posts: 12 Joined: 27-September 23 Member No.: 29,062 |
That sounds easy if the JSON values of total and LastWkt are numbers. But in https://forums.htmlhelp.com/index.php?showt...c=61470&hl= you mentioned that the JSON values could be dashes as well. What do you want the script to output in that case? I would want pship to display 0, not a - lastwkt could be a - though, at least until the first wicket falls This post has been edited by Cartoony: May 24 2024, 06:31 AM |
Christian J |
May 24 2024, 09:57 AM
Post
#7
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
Maybe something like this? Put it somewhere inside function getStatus(), I only tested the snippet in isolation.
CODE var pship_value=0; var total=formatdigit2s(data.total); var lastwkt=formatdigit2s(data.wickets); // pship_value is only recalculated if both JSON values are numbers, otherwise its default value remains unchanged (zero). if(typeof(total)==="number" && typeof(lastwkt)==="number") { pship_value=total-lastwkt; } document.getElementById('pship').innerHTML=pship_value; |
Cartoony |
May 29 2024, 05:45 AM
Post
#8
|
Newbie Group: Members Posts: 12 Joined: 27-September 23 Member No.: 29,062 |
Maybe something like this? Put it somewhere inside function getStatus(), I only tested the snippet in isolation. CODE var pship_value=0; var total=formatdigit2s(data.total); var lastwkt=formatdigit2s(data.wickets); // pship_value is only recalculated if both JSON values are numbers, otherwise its default value remains unchanged (zero). if(typeof(total)==="number" && typeof(lastwkt)==="number") { pship_value=total-lastwkt; } document.getElementById('pship').innerHTML=pship_value; Tiny bit of adjustment but that seems to have worked, thank you so much. |
Christian J |
May 29 2024, 09:15 AM
Post
#9
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
You're welcome!
|
Lo-Fi Version | Time is now: 3rd October 2024 - 05:20 AM |