Solución

@christamdev
·hace 3dTypeScript
solution.tsTypeScript
function binaryToDecimal(binary: string): number {
  // 101  = 1*2^0 + 0*2^1 + 1*2^2
  // 101 = 1 + 0 + 4
  // 101 = 5

  // (3 - 0) - 1 = 2
  // (3 - 1) - 1 = 1
  // (3 - 2) - 1 = 0
  const binaryArr = binary.split('').map((num) => Number(num));
  console.log('binaryArr: ', binaryArr);
  const sumTotal = binaryArr.reduce((prev, curr, index) => {
    console.log('prev: ', prev);
    console.log('curr: ', curr);
    console.log('index: ', index);
    console.log('-----------------------');
    return prev + curr * Math.pow(2,( (binaryArr.length - index) - 1 ));
  }, 0)

  return sumTotal;

}

// No modificar: necesario para evaluar el resultado.
export { binaryToDecimal };
0respuestas
Respuestas
0

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.
Markdown