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

130
Vistas
Sum answer not displaying

I have a program that I am currently trying to total.

The program is to have 10 numbers add into each text box and when the user hits the Sum button the program runs through the Add function and returns the total after the words 'Sum: '. The pages are linked with <script src="functions.js"></script> below the title tags.

Below are both pages.

function Add() {
  var add_entries = 0;
  for (i = 1; i <= 11; i++) {
    var textentry = "Text" + i;
    var x = Number(document.getElementById(textentry).value);
    add_entries += x;
  }
  console.log(add_entries);
  document.getElementById("Sum: ").innerHTML = add_entries;

}
Enter student test scores for all text boxes
<br>
<br>
<div>Blank responses will be treated as zeros.</div>
<br>
<br>
<div>Susan: <input id="Text1" type="text" /></div>
<div>Harry: <input id="Text2" type="text" /></div>
<div>Joe: <input id="Text3" type="text" /></div>
<div>Bill: <input id="Text4" type="text" /></div>
<div>Mary: <input id="Text5" type="text" /></div>
<div>Ken: <input id="Text6" type="text" /></div>
<div>Paul: <input id="Text7" type="text" /></div>
<div>John: <input id="Text8" type="text" /></div>
<div>Nora: <input id="Text9" type="text" /></div>
<div>Cindy: <input id="Text10" type="text" /></div>
<input id="Sum" type="button" value="Sum" onclick="Add()" />
<input id="Avg" type="button" value="Average" onclick="Avg()" />
<input id="High" type="button" value="Highest" onclick="Max()" />
<input id="Low" type="button" value="Lowest" onclick="Min()" />
<div>Sum: </div>
<div>Average: </div>
<div>Highest: </div>
<div>Lowest: </div>

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

0

for (i = 1; i <= 11; i++) 11 needs to be changed to 10 because you dont have Text11

document.getElementById("Sum: ").innerHTML = add_entries; ID is not correct

if you want the output set to your sum div you need to set an ID on it and put the id in document.getElementById(HERE)

EDIT Answer:

change this line

document.getElementById("Sum: ").innerHTML = add_entries;

to

document.getElementById("sumDiv").innerHTML = 'Sum: ' + add_entries;

and then add an ID to your div were you want the output to be

Like this <div id="sumDiv">Sum: </div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

document.getElementById("Sum: ").innerHTML = add_entries;

That's not a valid id. Give your div an id and use that.

<div id="mySum">Sum: </div>
document.getElementById("mySum").innerHTML = add_entries;

If you want to retain the "Sum:" you can use a span.

<div>Sum: <span id="mySum"></span></div>
document.getElementById("mySum").innerHTML = add_entries;
about 4 years ago · Juan Pablo Isaza Denunciar

0

The for statement goes one step too far, if you check the console you will see an error stating something like, "TypeError: document.getElementById(...) is null", thats because its checking id="Text11" change:

for (i = 1; i <= 11; i++)

to:

for (i = 1; i <= 10; i++)

and maybe even check that document.getElementById(textentry) doesn't return null before using it. And of course, "use parseInt instead of Number" as Ameer said, or use a plus sign :/

 +(document.getElementById(textentry).value);
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