how can I loop through a string and store or print its values wide? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how can I loop through a string and store or print its values wide?

hello everyone, I would like to ask a question, how could I separate a string but in pairs?, what I try to do is separate a hexadecimal code into even values and then do an operation and convert them to rgb, the issue is that I do not know how I could separate something like this: "#777777" in pairs, I would like to ask a question, how could I separate a string but in pairs?, what I try to do is separate a hexadecimal code into even values and then do an operation and convert them to rgb, the issue is that I do not know how I could separate something like this: "#777777" in pairs, could you give me any idea?. Gracias de antemano. Usé un traductor espero que no se vea tan mal.

14th Aug 2021, 5:28 AM
Sam Vásquez
Sam Vásquez - avatar
6 Answers
+ 4
Use substring method to extract the hex color code, and use parseInt to convert hex string to decimal number. Here an example to get red color code: const str = '#123456'; // to get Red value const Rhex = str.substring(1,3); // extract substring from position index 1 to 2 const Rint = parseInt(Rhex, 16); // convert to decimal console.log('Rhex = ' + Rhex); // expected output: "Rhex = 12" console.log('Rint = ' + Rint); // expected output: "Rint = 18" Sam Vásquez You assignment now, please calculate hex and decimal values of green and blue color using the above example.
14th Aug 2021, 11:04 AM
Calviղ
Calviղ - avatar
+ 3
Sam Vásquez Just get G and B values by using my example first. Once you have RGB values, I will show you the way to get them set in an array.
14th Aug 2021, 11:06 PM
Calviղ
Calviղ - avatar
+ 1
Sam Vásquez I've already transformed the R. But I don't show a full solution. I want you to transform G and B in order you can grasp the knowledge.
14th Aug 2021, 4:32 PM
Calviղ
Calviղ - avatar
0
Calviղ if thanks, but that transforms it to rgb?, I seek to transform a hexadecimal into rgb, in addition, I also seek to do it several times, without being so specific with the substring
14th Aug 2021, 2:56 PM
Sam Vásquez
Sam Vásquez - avatar
0
Calviղ what I want to know is how I can divide a string into pairs and save it in an array, the conversion operation I saw in a post and to be able to do it, I need to divide the hexadecimal into pairs
14th Aug 2021, 4:47 PM
Sam Vásquez
Sam Vásquez - avatar
0
okay I'll try it with the examples to see how it goes, thanks 👌
15th Aug 2021, 6:09 AM
Sam Vásquez
Sam Vásquez - avatar