I am learning some new concept in JavaScript. Can some body help me to achieve the following results.
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'));
2:
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'));
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.]+).Number function to them.+=, create an array with the products and then join these into a multi-line text to assign (once) to the field.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>