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>

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

How is this answer any different than mine?
It's not. We both started posting when the question had no answers, I would guess.
Uh, no. My answer was posted 4 minutes before yours.
Thank u very much
@ScottMarcus, yes, but you shouldn't have given that answer at all. Rather should've closed the question as a duplicate and/or tailored the answer to the OP's specifics, like Dryden Long did. You've got 20+K and a dupe hammer. Eric does not.
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

3 Comments

I tried this code before but i couldnt make it work on my coding :/
But, as you can see, this is the way and it works. What, in your code, contains the string that you need the last 4 chars of?
Thank u very much
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>

2 Comments

Thank u so much i so my mistake , i will give u the green button in 8 minutes :)
I have another problem. when data-id was changing code selecting old values not research again on data ids. Do u have idea ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.