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

124
Vistas
How do I incorporate parseInt into this code?

I've got almost no experience with Javascript and I need make a "calculator" type website. This is the part of the code that is suppose to calculate, but it only adds values together instead of calculating them.

I was told parseInt will fix this, but due to my inexperience I can't find how to do so through Google.

Any help is appreciated and explenations are welcome. Thanks!

    function izracunaj() {
      const st1 = document.getElementById("prvoStevilo").value;
      //določim konstantno ki bo pridobila informacije iz polja kjer smo določili id
      const st2 = document.getElementById("drugoStevilo").value;
      const operacija = document.getElementById("operacija").value;

      let rezultat;

      switch (operacija) {
        //v oklepaje vpišemo operacija, saj to preverjamo
        case '+':
        rezultat = st1+st2;
        break;
        //podatki se skranijo v rezultat
        case '-':
        rezultat = st1-st2;
        break;
        case '*':
        rezultat = st1*st2;
        break;
        case '/':
        rezultat = st1/st2;
        break;
      }
      document.getElementById("rezultat").value = rezultat;
    }
<!DOCTYPE html>
<html>

  <head>
    <title>Kalkulator</title>
    <script src="javascript.js"></script>
  </head>

  <body>
    <h1>Kalkulator</h2>
    <form>
      Prvo število: <br>
      <input type="text" id="prvoStevilo"> <br>
      Drugo število: <br>
      <input type="text" id="drugoStevilo"> <br>
      Operacija: <br>
      <input type="text" id="operacija"> <br>
      Rezultat: <br>
      <input type="text" id="rezultat" disabled="true"> <br>
      <br><br>
      <input type="button" value="Izračunaj" onclick="izracunaj()">
    </form>
  </body>

</html>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

When you use .value then it will give you the value of type string. All you have to do is either use parseInt or use + to convert it into number type

parseInt(document.getElementById("prvoStevilo").value, 10); 

or

+document.getElementById("prvoStevilo").value; 

or

Number(document.getElementById("prvoStevilo").value); 

function izracunaj() {
  const st1 = parseInt(document.getElementById("prvoStevilo").value);   // change
  //določim konstantno ki bo pridobila informacije iz polja kjer smo določili id
  const st2 = +document.getElementById("drugoStevilo").value; // change
  const operacija = document.getElementById("operacija").value;

  let rezultat;

  switch (operacija) {
    //v oklepaje vpišemo operacija, saj to preverjamo
    case '+':
      rezultat = st1 + st2;
      break;
      //podatki se skranijo v rezultat
    case '-':
      rezultat = st1 - st2;
      break;
    case '*':
      rezultat = st1 * st2;
      break;
    case '/':
      rezultat = st1 / st2;
      break;
  }
  document.getElementById("rezultat").value = rezultat;
}
<h1>Kalkulator</h2>
  <form>
    Prvo število: <br>
    <input type="text" id="prvoStevilo"> <br> Drugo število: <br>
    <input type="text" id="drugoStevilo"> <br> Operacija: <br>
    <input type="text" id="operacija"> <br> Rezultat: <br>
    <input type="text" id="rezultat" disabled="true"> <br>
    <br><br>
    <input type="button" value="Izračunaj" onclick="izracunaj()">
  </form>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could also let javascript evaluate the equation for you using Function. I multiply the first two arguments by 1 to force them to either NaN or numeric values, and limit the operator to the four values in the regex in order to sanitize the inputs, then combine the arguments into a string with a template literal.

function izracunaj() {
      
  const st1 = document.getElementById("prvoStevilo").value*1;
  const st2 = document.getElementById("drugoStevilo").value*1;
  const operacija = document.getElementById("operacija").value;
      
  if(!/[+-/*]/.test(operacija)) { return; }
      
  let formula = `return ${st1}${operacija}${st2}`;
  let rezultat = new Function(formula);
      
  document.getElementById("rezultat").value = rezultat();
      
}
<!DOCTYPE html>
<html>

  <head>
    <title>Kalkulator</title>
    <script src="javascript.js"></script>
  </head>

  <body>
    <h1>Kalkulator</h2>
    <form>
      Prvo število: <br>
      <input type="text" id="prvoStevilo"> <br>
      Drugo število: <br>
      <input type="text" id="drugoStevilo"> <br>
      Operacija: <br>
      <input type="text" id="operacija"> <br>
      Rezultat: <br>
      <input type="text" id="rezultat" disabled="true"> <br>
      <br><br>
      <input type="button" value="Izračunaj" onclick="izracunaj()">
    </form>
  </body>

</html>

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