Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda