Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  59] [ 4]  / answers: 1 / hits: 6353  / 11 Years ago, tue, february 4, 2014, 12:00:00

I'm populating a container with different symbols using an icon font. I want to know if there is a better way to iterate through hexadecimal values than to create a custom array and do it like this:



var hexPlaceValue1=0, hexPlaceValue2=0;
var hexArray = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];

for(var i=0;i through something;i++){
if(hexPlaceValue1 == 15) {
var $glyph = $('<div class=glyph data-glyph-index=' + i + '>&#xe' + String('00' + hexArray[hexPlaceValue2] + hexArray[hexPlaceValue1]).slice(-3) + ';</div>');
hexPlaceValue1 = 0;
hexPlaceValue2++;
} else {
var $glyph = $('<div class=glyph data-glyph-index=' + i + '>&#xe' + String('00' + hexArray[hexPlaceValue2] + hexArray[hexPlaceValue1]).slice(-3) + ';</div>');
hexPlaceValue1++;
}
}


Obviously, this can lead to problems if more icons are introduced (granted, it would have to be a lot.) I just want to know if there is an more efficient way of doing this.


More From » jquery

 Answers
5

Based on the logic in your for loop, it looks essentially like you want to do:



for(var i=0;i through something;i++) {
var hex = '&#x' + (0xe000|i).toString(16);
var $glyph = $('<div class=glyph data-glyph-index='+i+'>'+hex+';</div>');
}


As long as i<4096 (0x1000) then the most significant hex digit remains as e.


[#48060] Tuesday, February 4, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harrisonnelsonb

Total Points: 63
Total Questions: 112
Total Answers: 97

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;