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

168
Vistas
How do I ge the values from input type="Text" and then average it using js?

I want to get the average from input type="text". In the input type text, it has the format 1,2,3. When they click on the button it should alert them the average. Here is my code:

In the input type text in the snippet type in 1,2,3. Then you will see the problem.

function averagevalue(){
var numberstouse = document.getElementById('average').value;
const average = array => array.reduce((a, b) => a + b) / array.length;
alert(average([numberstouse]));
}
<input type="text" id="average" value>
<button type="btn" onclick="averagevalue()">Avg</button>

But the problem is, as you can see in the snippet, it says the value NaN. But in the alert, I want to says the average. Any help is appreciated.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Use Split to convert the input to an array

The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

function averagevalue(){
var numberstouse = document.getElementById('average').value;
const average = array => array.reduce((a, b) => Number(a) + Number(b)) / array.length;
alert(average(numberstouse.split(',')));
}
<input type="text" id="average" value=>
<button type="btn" onclick="averagevalue()">Avg</button>

about 4 years ago · Santiago Trujillo Denunciar

0

The value is a comma seperated string. so we use the .split() method on it with, , as an argument to that method and that will return a list without the comma.

Edit : I forgot to convert the items in list to number. fixed !

function averagevalue(){
var numberstouse = document.getElementById('average').value;
numberstouse = numberstouse.split(',')
numberstouse = numberstouse.map(str => Number(str));
const average = array => array.reduce((a, b) => a + b) / array.length
alert(average(numberstouse));
}
<input type="text" id="average" value=>
<button type="btn" onclick="averagevalue()">Avg</button>

about 4 years ago · Santiago Trujillo Denunciar

0

you need to pass the whole input and split it to make a new array, and its elements to floats you could use this instead :

function averagevalue(){
var numberstouse = document.getElementById('average').value;
const average = numberstouse =>
{
    let newArr = numberstouse.split(",");
    let val = newArr.reduce((a,b)=>parseFloat(a)+parseFloat(b)) / newArr.length;
    return val;
} 
alert(average(numberstouse));
}
about 4 years ago · Santiago Trujillo 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