Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
¿Cómo obtengo los valores del tipo de entrada = "Texto" y luego promedio usando js?

Quiero obtener el promedio de input type="text". En el texto de tipo de entrada, tiene el formato 1,2,3. Cuando hacen clic en el botón, debería alertarlos sobre el promedio. Aquí está mi código:

En el texto de tipo de entrada en el tipo de fragmento en 1,2,3. Entonces verás el problema.

 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>

Pero el problema es que, como puede ver en el fragmento, dice el valor NaN. Pero en la alerta, quiero decir el promedio. Cualquier ayuda es apreciada.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use Split para convertir la entrada en una matriz

El método split() toma un patrón y divide una cadena en una lista ordenada de subcadenas buscando el patrón, coloca estas subcadenas en una matriz y devuelve la matriz.

 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 Report

0

El valor es una cadena separada por comas. entonces usamos el método .split() con, , como argumento para ese método y eso devolverá una lista sin la coma.

Editar: olvidé convertir los elementos de la lista en número. fijado !

 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 Report

0

necesita pasar toda la entrada y dividirla para crear una nueva matriz, y sus elementos para flotar, podría usar esto en su lugar:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!