2

This code chooses the highest numerical value between the data ids and adds an lime background to it. btw these numbers are hex values. my problem is that in hex values its the matter of the last 4 values but my code takes the whole characters. how can i make my code work for only the last 4 characters?

Im sorry this might be easy to solve for you but i tried over and over again and couldnt make it work.

Hex to decimal:

dc61 = 56417

dc62 = 56418

dc63 = 56419

dc64 = 56420

 maxData = $(".answers li[data-id]").get ().reduce  ( (maxObj, crrntNode) => {
 
    var idVal   = parseInt ( $(crrntNode).data("id"), 16) ; 

    if (idVal > maxObj.value) {
        maxObj.value  = idVal;
        maxObj.node   = crrntNode;
    }
    return maxObj;
  },
  {value: 0, node: null}
);
$("body").append (`<p>The highest data-id value was ${maxData.value}.</p>`)
$(maxData.node).css ("background", "lime");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="question-text" class="question sp-element border-radius active">What is favorite colour?</div>
          <ul class="answers" id="answers">
            <li data-id="58b9890062279282090ddc61" class="answer sp-element border-radius active">Purple</li>
            <li data-id="58b9890062279282090ddc63" class="answer sp-element border-radius active">Blue</li>
            <li data-id="58b9890062279282090ddc64" class="answer sp-element border-radius active">Yellow</li>
            <li data-id="58b9890062279282090ddc62" class="answer sp-element border-radius active">Red</li>
          </ul>
Nur Bar
  • 75
  • 1
  • 1
  • 6
  • Is the desired result different than what this previous SO answer provides? https://stackoverflow.com/questions/5873810/how-can-i-get-last-characters-of-a-string-using-javascript – pastaleg Dec 20 '17 at 21:37

3 Answers3

9

If I understand your question correctly (and I'm not sure I do), you just want to take the last 4 characters of a known string (to translate into decimal, but that wasn't your question).

In javascript, you would want to use the substring api. For example, to get the last 4 digits of an arbitrary string, you could do this:

var str = "abcdefghijklmnop";
var substr = str.substring(str.length-4, str.length);

More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

Eric
  • 433
  • 5
  • 12
3

Use the String.substr(startPosition, numChars) method:

var myString = "abcdefg";
console.log(myString.substr(-4, 4)); // Start 4 chars before end and take 4 chars
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
2

By using substr you can get the last 4 characters of the data attribute. So, instead of:

var idVal = parseInt($(crrntNode).data("id"), 16);

You can do something like this:

var nodeId = $(crrntNode).data("id");
var idVal = parseInt(nodeId.substr(nodeId.length - 4), 16);

 maxData = $(".answers li[data-id]").get ().reduce  ( (maxObj, crrntNode) => {
    var nodeId = $(crrntNode).data("id");
    var idVal = parseInt(nodeId.substr(nodeId.length - 4), 16); 

    if (idVal > maxObj.value) {
        maxObj.value  = idVal;
        maxObj.node   = crrntNode;
    }
    return maxObj;
  },
  {value: 0, node: null}
);
$("body").append (`<p>The highest data-id value was ${maxData.value}.</p>`)
$(maxData.node).css ("background", "lime");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="question-text" class="question sp-element border-radius active">What is favorite colour?</div>
          <ul class="answers" id="answers">
            <li data-id="58b9890062279282090ddc61" class="answer sp-element border-radius active">Purple</li>
            <li data-id="58b9890062279282090ddc63" class="answer sp-element border-radius active">Blue</li>
            <li data-id="58b9890062279282090ddc64" class="answer sp-element border-radius active">Yellow</li>
            <li data-id="58b9890062279282090ddc62" class="answer sp-element border-radius active">Red</li>
          </ul>
Dryden Long
  • 10,072
  • 2
  • 35
  • 47
  • 1
    Thank u so much i so my mistake , i will give u the green button in 8 minutes :) – Nur Bar Dec 20 '17 at 21:41
  • I have another problem. when data-id was changing code selecting old values not research again on data ids. Do u have idea ? – Nur Bar Dec 20 '17 at 22:17