Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
Converts RGB values into Hex values without using toString(16)

I need my function to convert RGB decimal values into Hexadecimal values without using toString(16). This is what I have. Why isn't it working? Is it because I have more than one switch statement so it completes the iteration without completing the second one?

function rgb(r, g, b){
 let result = '';
 // Convert into array for iteration
 let arr = [r,g,b];
 // Iterate through each color
 for(let i=0; i<arr.length; i++) {
  if (arr[i] > 255) result += 'FF';
  if (arr[i] < 0) result += '00';

 // Convert first iteration into hex format
  switch (Math.floor(arr[i]/16)) {
   case 10: result += 'A';break;
   case 11: result += 'B';break;
   case 12: result += 'C';break;
   case 13: result += 'D';break;
   case 14: result += 'E';break;
   case 15: result += 'F';break;
   default: result += (Math.floor(arr[i]/16) + '');
  }

 // Convert second iteration into hex format
  switch (((arr[i]/16)-16)*16) {
   case 10: result += 'A';break;
   case 11: result += 'B';break;
   case 12: result += 'C';break;
   case 13: result += 'D';break;
   case 14: result += 'E';break;
   case 15: result += 'F';break;
   default: result += ((((arr[i]/16)-16)*16) + '');
  }
 }
return result;
}

// function should return the following results.
rgb(255, 255, 255) // returns FFFFFF
rgb(255, 255, 300) // returns FFFFFF
rgb(0,0,0) // returns 000000
rgb(148, 0, 211) // returns 9400D3
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

A problem is that, when turning a number (eg, 255) into two characters, calculating the second character by using

((arr[i]/16)-16)*16

is not the right calculation. You want a range of 0 to 15 - taking out the multiples of 16 until you're left with 0 to 15. Just use % 16.

Another problem is that you're switching even if past the range of 0 to 255 - break instead so that the rest of the loop body does not execute in such cases.

function rgb(r, g, b){
 let result = '';
 // Convert into array for iteration
 let arr = [r,g,b];
 // Iterate through each color
 for(let i=0; i<arr.length; i++) {
  if (arr[i] > 255) {
    result += 'FF';
    break;
  }
  if (arr[i] < 0) {
    result += '00';
    break;
  }
 // Convert first iteration into hex format
  switch (Math.floor(arr[i]/16)) {
   case 10: result += 'A';break;
   case 11: result += 'B';break;
   case 12: result += 'C';break;
   case 13: result += 'D';break;
   case 14: result += 'E';break;
   case 15: result += 'F';break;
   default: result += (Math.floor(arr[i]/16) + '');
  }
 // Convert second iteration into hex format
  switch (arr[i] % 16) {
   case 10: result += 'A';break;
   case 11: result += 'B';break;
   case 12: result += 'C';break;
   case 13: result += 'D';break;
   case 14: result += 'E';break;
   case 15: result += 'F';break;
   default: result += arr[i] % 16;
  }
 }
return result;
}

console.log(rgb(255, 255, 255)) // returns FFFFFF
console.log(rgb(255, 255, 300)) // returns FFFFFF
console.log(rgb(0,0,0)) // returns 000000
console.log(rgb(148, 0, 211)) // returns 9400D3

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda