0

I'm new to coding and I've been searching for hours and haven't really found a definitive answer to my problem. A few suggestions have been close to what I want to achieve which I think has helped a little but still not getting the outcome I want.

I've been using codepen.io a lot for seeing an instant output to my code as opposed to jsfiddle, just because I prefer how it works.

This is the code in question:

    var x;
    var y;
    var z;

    var arrayFiller;
    var betaArray = new Object(256);
    betaArray[0] = 0 + " " + 0;

    for(var i=1; i<256; i++)
    {
      x = i;
      y = x % 16;
      x = x / 16;
      x = Math.floor(x);
      z = x % 16;
      x = i;

      arrayFiller = z + "" + y + " ";

      $(
        function()
        {
          var hexDerp = 
              {
                '0' : "0",
                '1' : "1",
                '2' : "2",
                '3' : "3",
                '4' : "4",
                '5' : "5",
                '6' : "6",
                '7' : "7",
                '8' : "8",
                '9' : "9",
                '10': "A",
                '11': "B",
                '12': "C",
                '13': "D",
                '14': "E",
                '15': "F"
              };
          var hexDerp1 = /(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15)/g;
          var arrayFillerHex = arrayFiller.replace
                                                   (
                                                     hexDerp1,
                                                     function(s)
                                                     {
                                                       if(s in hexDerp)
                                                         {
                                                           return hexDerp[s];
                                                         }
                                                     }
                                                   );
       }
     );
     betaArray[i] = arrayFiller;
     document.write(betaArray[i]);
   }

My apologies if it is poorly formatted, I find this to be the clearest method for myself. The bit that currently doesn't work is the function part which is an amalgamation of what I've found in order to replace the 10-15 with a-f.

There may be other ways of getting this inputted and outputted, but I want to keep this for what I am going to end up using this for.

tl;dr: what I wanted to do with this code, is get an array that is 256 elements large, and populate the elements with the hexadecimal version of the element number, to later be used in a unique alphabet that I am making.

Zephyr
  • 167
  • 1
  • 8
  • 1
    so I am guessing the problem you are trying to solve is to convert decimal in hexadecimal equivalent ? – Aditya Jain Jun 03 '14 at 14:37
  • 2
    My suggestion would be to ask for a better solution, and not discard them with that last line in the question, as this solutions seems to suck balls. – adeneo Jun 03 '14 at 14:40
  • Correct me if I'm wrong, but it looks like you want to convert decimal to hexadecimal. Similar to what 'theonlygusti' says below, check out this stackoverflow thread on converting decimal to hex: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript – Alex Johnson Jun 03 '14 at 14:45
  • Welcome to SO! Thanks for posting your code, you just forgot to tell us about the actual problem you're facing. Making people guess is never a good way to get help. – georg Jun 03 '14 at 14:52
  • Hey, thanks for the advice. I know that sometimes I can circumvent the actual thing I want to do, I tend to overthink stuff a lot. Basically what I wanted to do with this code, is get an array that is 256 elements large, and populate the elements with the hexadecimal version of the element number, to later be used in a unique alphabet that I am making. It is also my attempt to teach myself object orientated coding as a practical exercise. I'll edit the question to clarify what I wanted to ask. I also wanted to make sure that the math was working the way I wanted it to work as well. – Zephyr Jun 03 '14 at 21:50

1 Answers1

1

You can convert decimal numbers to hexadecimal numbers using this:

(anyNumber).toString(16);
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • I wanted to use the method that I've done above because I will be using it later on in my code as well: i.e. the replace. However anything that I searched up was pretty foreign to me, so I'm not sure I could understand it properly. – Zephyr Jun 05 '14 at 08:00