Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with .reduce() method to multiply or divide

I’m trying to use reduce to operate this calculator but I’m having some issues when I’m trying to multiply or divide the numbers. The result of the multiplication is 0 and the result of the division is Infinity.
I’m getting always a 0 in the array and I tried to use the method .pop() to delete that 0 but is not working. Hope some one can help me.

Thank you!

let resultadosObtenidos = [];
let numerosIntroducidos = [];

function iniciamosCalculadoraPRO() {
  const introducirNum = () => {
    let datosObtenidos = [];
    let datos;
    do {
      datos = Number(prompt('Introduce un número.'));
      if (isNaN(datos)) {
        alert('Debes introducir solo números NO letras.');
        return introducirNum();
      } else {
        datosObtenidos.push(datos);
      }
    }
    while (datos !== 0)
    return datosObtenidos
  }
  numerosIntroducidos = introducirNum();

  const operacionesCalculadora = () => {
    let operaciones = [];
    if (numerosIntroducidos.length > 1) {
      const suma = numerosIntroducidos.reduce((valorInicial, segundoValor) => valorInicial + segundoValor);
      const resta = numerosIntroducidos.reduce((valorInicial, segundoValor) => valorInicial - segundoValor);
      const multiplicacion = numerosIntroducidos.reduce((valorInicial, segundoValor) => valorInicial * segundoValor);
      const division = numerosIntroducidos.reduce((valorInicial, segundoValor) => valorInicial / segundoValor);



      operaciones.push('La suma es: ' + suma + ' La resta es: ' + resta + ' La multiplicación es: ' + multiplicacion + ' La división es: ' + division);
      return operaciones;
    }
  }
  resultadosObtenidos = operacionesCalculadora();



  const mostramosResultadosOperaciones = () => {
    if (numerosIntroducidos.length > 2) {
      alert("Los resultados son: " + resultadosObtenidos);
    }
  }
  mostramosResultadosOperaciones();


  const repetimosProceso = () => {
    if (confirm("Quieres volver hacer calculos?")) {
      return iniciamosCalculadoraPRO();
    } else {
      alert("Gracias por usar nuestra calculadora PRO.");
    }
  }
  repetimosProceso();
}
iniciamosCalculadoraPRO();

I want to understand how to delete that 0 of the array and finally get my correct results in the calculator

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re using 0 to end the list of inputs, but you’re also pushing that onto the datosObtenidos array. When you multiply by 0, you get 0. When you divide by 0, you get infinity.

The value that you’re using to terminate the input shouldn’t be pushed onto the array.

Also, you shouldn’t call introducirNum() recursively when you get an invalid input, since that will discard all the previous inputs. Use a nested loop for invalid inputs.

let resultadosObtenidos = [];
let numerosIntroducidos = [];

function iniciamosCalculadoraPRO() {
  const introducirNum = () => {
    let datosObtenidos = [];
    let datos;
    while (true) {
      while (true) {
        datos = Number(prompt('Introduce un número.'));
        if (isNaN(datos)) {
          alert('Debes introducir solo números NO letras.');
        } else {
          break;
        }
      }
      if (datos == 0) {
        break;
      } else {
        datosObtenidos.push(datos);
      }
    }
    return datosObtenidos
  }
  numerosIntroducidos = introducirNum();

  const operacionesCalculadora = () => {
    let operaciones = [];
    if (numerosIntroducidos.length > 1) {
      const suma = numerosIntroducidos.reduce((valorInicial, segundoValor) => valorInicial + segundoValor);
      const resta = numerosIntroducidos.reduce((valorInicial, segundoValor) => valorInicial - segundoValor);
      const multiplicacion = numerosIntroducidos.reduce((valorInicial, segundoValor) => valorInicial * segundoValor);
      const division = numerosIntroducidos.reduce((valorInicial, segundoValor) => valorInicial / segundoValor);



      operaciones.push('La suma es: ' + suma + ' La resta es: ' + resta + ' La multiplicación es: ' + multiplicacion + ' La división es: ' + division);
      return operaciones;
    }
  }
  resultadosObtenidos = operacionesCalculadora();



  const mostramosResultadosOperaciones = () => {
    if (numerosIntroducidos.length > 2) {
      alert("Los resultados son: " + resultadosObtenidos);
    }
  }
  mostramosResultadosOperaciones();


  const repetimosProceso = () => {
    if (confirm("Quieres volver hacer calculos?")) {
      return iniciamosCalculadoraPRO();
    } else {
      alert("Gracias por usar nuestra calculadora PRO.");
    }
  }
  repetimosProceso();
}
iniciamosCalculadoraPRO();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading