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

319
Vistas
How to use Foreach in JavaScript to do multiplication of 2 multiline textboxes

I am learning some new concept in JavaScript. Can some body help me to achieve the following results.

enter image description here

Values1(this.getField("Text6")) is a multiline textbox

Values2(this.getField("Text7")) is a multiline textbox

Result (this.getField("Text8")) is the product of Value 1 and Value2

Tested the following code to display only the Values 2 in Result box by using foreach method.But I am not sure how to perform multiplication within it?

var values1 = this.getField("Text6").value.split('\r');
var values2 = this.getField("Text7").value.split('\r');
this.getField("Text8").value = "";

values2.forEach(element => this.getField("Text8").value +=(element+'\n'));

Current output: enter image description here

2:

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

0

The second argument to the forEach callback is the array index. You can use that to index into values1, so you can multiply them.

values2.forEach((element, index) => 
    this.getField("Text8").value +=(element * values1[index] + '\n'));
about 4 years ago · Juan Pablo Isaza Denunciar

0

getField is not a native JavaScript function. I'll just define it in the snippet below.

Some remarks:

  • split('\r') may not be the best way to get the lines, as there might be \n characters in there too. I would instead match numbers with a regular expression ([\d.]+).
  • As this split up produces strings, I would immediately convert them to numbers by applying the Number function to them.
  • Instead of accumulating the result with +=, create an array with the products and then join these into a multi-line text to assign (once) to the field.
  • Use the index you can get during the loop, and with that index get the other number to multiply with

var getField = document.getElementById.bind(document);

var collect = id => (this.getField(id).value.match(/[\d.]+/g) || []).map(Number);

var values1 = collect("Text6");
var values2 = collect("Text7");
this.getField("Text8").value = values2.map((element, i) =>  element*values1[i]).join("\n");
textarea {
  width: 5em;
  height: 150px;
  margin-right: 30px;
}
<textarea  id=Text6>
1
2
3
4
</textarea>
<textarea  id=Text7>
4
5
6
7
</textarea><textarea  id=Text8></textarea>

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