1

I have this code:

    document.getElementById('numsco').value = ($.trim(data.nscore));

The value of nscore is 140.25. It displays as:

140  2/8

for sorting purposes, it needs to display as:

140 2/8

Is there a way to force that extra space to go away?

replace(" ", " ") does not work, nor do any of several regex solutions I've found online.

This is not a duplicate of the above question, as I have used that code here:

document.getElementById('numsco').value = ($.trim(data.nscore));
document.getElementById('numsco').value.replace(/\s/g, "");
alert(document.getElementById('numsco').value);

and it returns this:

screenshot

The problem appears to be related to the way whatever tool is used to display the data does so.

Here is the Razor code:

<td hidden="hidden">@Html.EditorFor(model => model.tblMeasurement.num, new { htmlAttributes = new { @class = "form-control", id = "numsco", @readonly = "readonly", tabindex = "58000", Style = "border-color:none" } })</td>

FOUND A FIX, IF NOT A REAL SOLUTION:

The original coder wrote this monstrosity:

            List<tblReglist> reglist = new List<tblReglist>();

            var cnty = (from a in be.FIPSCountyCodes
                        where a.StateCode == 47
                        select new
                        {
                            county = a.CountyName,
                            countyid = a.CountyCode,
                            state = a.StateCode
                        }).ToArray();


            var regtbl = (from reg in db.tblReglists
                          join mes in db.tblMeasurements
                          on reg.ID equals mes.ID
                          where reg.IsUsed == true
                          && (mes.IsUsed == true)
                          && (reg.regeligible == "Y")
                          && (mes.IsUsed == true)
                          //restrictions
                          && (reg.weapon.Contains("Bow") && (mes.typ.Contains("Typ") && mes.numScore >= 115)
                          || (reg.weapon.Contains("Bow") && (mes.typ.Contains("NonTyp") && mes.numScore >= 140))
                          || (reg.weapon.Contains("Muzzleloader") && (mes.typ.Contains("NonTyp") && mes.numScore >= 150))
                          || (reg.weapon.Contains("Muzzleloader") && (mes.typ.Contains("Typ") && mes.numScore >= 125))
                          || (reg.weapon.Contains("Gun") && (mes.typ.Contains("Typ") && mes.numScore >= 140))
                          || (reg.weapon.Contains("Gun") && (mes.typ.Contains("NonTyp") && mes.numScore >= 165)))
                          orderby mes.numScore descending
                          select new
                          {
                              scored = mes.abtotal8,
                              type = mes.typ,
                              weapons = reg.weapon,
                              county = reg.CountyId,
                              harvestdate = reg.harvestDate,
                              frstname = reg.firstName,
                              lstname = reg.lastName,
                              nscre = mes.numScore

                          }).ToArray();

            var query = from r in regtbl
                        join c in cnty
                        on r.county equals c.countyid
                        orderby r.nscre descending
                        select new
                        {
                            firstName = r.frstname,
                            score = r.scored,
                            typ = r.type,
                            weapon = r.weapons,
                            countys = c.county,
                            harvdate = r.harvestdate,
                            lstnme = r.lstname
                        };


            foreach (var item in query)
            {
                reglist.Add(new tblReglist()
                {
                    city = item.score.ToString(),
                    street = item.typ,
                    weapon = item.weapon,
                    state = item.countys,
                    firstName = item.firstName,
                    lastName = item.lstnme,
                    harvestDate = item.harvdate
                });
            }

            return View(reglist);

It's a horrendous mess of spaghetti, but with the help of a couple of coworkers, we followed it through and found that it's something with the way the ToString() function is modifying the value here:

city = item.score.ToString()

Adding

.Replace("  ", " ")

...after the ToString() made it display properly.

I despise working on other programmers' code.

Carthax
  • 119
  • 1
  • 12
  • where do you get the value from? btw, why `'2/8'` if you have `'1/4'`? – Nina Scholz Nov 25 '19 at 21:58
  • @NinaScholz I get the value from a database, and its value all the way through to the above code is 140.25. As for the '2/8', well, it was like that when I inherited it, and apparently it's the way this state has scored deer antlers for years. – Carthax Nov 25 '19 at 22:00
  • In order for us to give you a better approach in how to fix it, you should provide us the understanding of how you get every result. This means, what does `data.score` contains, do you use any other functions to get the current result?, etc.. – k3llydev Nov 25 '19 at 22:05
  • @k3llydev -- all of my code surrounding this field is now in the question. – Carthax Nov 26 '19 at 14:19

1 Answers1

0

The replace(" ", " ") would replace every space.. with a space. You could replace any amount of spaces with just one space:

let fraction = "140  2/8"
fraction.replace(/ +/g, ' ') // '140 2/8'

/ +/g is a regex that matches one or more spaces globally. In the replace method these 1 (or more) spaces are replaced by just one, basically truncating the amount of spaces to always be one.

Daniel_I_Am
  • 196
  • 1
  • 5
  • I have two spaces in the original text; it just doesn't display properly in a website because I didn't type an   to add the extra. – Carthax Nov 26 '19 at 14:20