# Javascript script to compute calculated field

**URL:** <https://discuss.elastic.co/t/javascript-script-to-compute-calculated-field/19522>\
**Category:** Elasticsearch\
**Created:** [August 29, 2014, 2:56am UTC](https://discuss.elastic.co/t/javascript-script-to-compute-calculated-field/19522 "2014-08-29T02:56:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smitha\_Gowda](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Smitha\_Gowda](https://discuss.elastic.co/u/Smitha_Gowda)\
**Post date:** [August 29, 2014, 2:56am UTC](https://discuss.elastic.co/t/javascript-script-to-compute-calculated-field/19522/1 "2014-08-29T02:56:32Z")

</div>

Hello,

I have JavaScript plugin installed. I can use inline javascript for script  
property, but cannot reference a javascript file name similar to article.

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html)

I can put a groovy script in there and reference it just fine.

Can you help me, this is my javascript, maybe even help convert into groovy

calculate-scenariotime(match);  
function calculate-scenariotime(match) {  
var elapsedtime = new Array();  
var cStart;  
var j = 0;  
for (var i = 0; i \< \_source.events.length; i++) {  
if (\_source.events[i].name === match[j]) {  
if (j === 0) {  
cstart = \_source.events[i].start;  
}  
if (j === match.length - 1) {  
var dur = (Date.parse(\_source.events[i].end) -  
Date.parse(cstart)) / 1000;  
elapsedtime[elapsedtime.length] = dur;  
j = -1;  
}  
j++;  
}  
else {  
j = 0;  
}  
}  
return elapsedtime;  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1da8db19-4026-4df8-9812-3d0865fc1ce5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1da8db19-4026-4df8-9812-3d0865fc1ce5%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:05am UTC](https://discuss.elastic.co/t/javascript-script-to-compute-calculated-field/19522/2 "2017-07-06T01:05:33Z")

</div>

Closed on July 6, 2017, 1:05am UTC


