Seems Arcade doesn't yet have a math function, like, say: Mod(value, modulo) -> Number
Anyone have a creative function for calculating one using the existing math functions?
Thanks!
User error. Arcade doesn't need a Mod() function. It already has a "%" Modulus operator.
Hi Jim Barry ,
Nice to see you are doing some Arcade! Normally there are several ways that lead to Rome. Here is an example to play around with.
<SPAN class="keyword token">var</SPAN> amount <SPAN class="operator token">=</SPAN> <SPAN class="number token">199.99</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> values <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"Penny"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0.01</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Nickel"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0.05</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Dime"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0.1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Quarter"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0.25</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"One Dollar"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Five Dollars"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Ten Dollars"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Twenty Dollars"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">20</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Fifty Dollars"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">50</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"One Hundred Dollars"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">100</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> currencies <SPAN class="operator token">=</SPAN> <SPAN class="token function">Reverse</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Penny"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Nickel"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Dime"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Quarter"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"One Dollar"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Five Dollars"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Ten Dollars"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Twenty Dollars"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Fifty Dollars"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"One Hundred Dollars"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">var</SPAN> result <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Pay $ "</SPAN> <SPAN class="operator token">+</SPAN> amount <SPAN class="operator token">+</SPAN> <SPAN class="string token">":"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">var</SPAN> i <SPAN class="keyword token">in</SPAN> currencies<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">var</SPAN> value <SPAN class="operator token">=</SPAN> values<SPAN class="punctuation token">[</SPAN>currencies<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>i <SPAN class="operator token">==</SPAN> <SPAN class="token function">Count</SPAN><SPAN class="punctuation token">(</SPAN>currencies<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">var</SPAN> coinsbills <SPAN class="operator token">=</SPAN> <SPAN class="token function">Round</SPAN><SPAN class="punctuation token">(</SPAN>amount <SPAN class="operator token">/</SPAN> value<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">var</SPAN> coinsbills <SPAN class="operator token">=</SPAN> <SPAN class="token function">Floor</SPAN><SPAN class="punctuation token">(</SPAN>amount <SPAN class="operator token">/</SPAN> value<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>coinsbills <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> result <SPAN class="operator token">+</SPAN><SPAN class="operator token">=</SPAN> TextFormatting<SPAN class="punctuation token">.</SPAN>NewLine <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> coinsbills <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> currencies<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// amount -= coinsbills * value;</SPAN> amount <SPAN class="operator token">=</SPAN> <SPAN class="token function">Round</SPAN><SPAN class="punctuation token">(</SPAN>amount<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN>coinsbills <SPAN class="operator token">*</SPAN> value<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">return</SPAN> result<SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This will return:
Pay $ 199.99: 1 One Hundred Dollars 1 Fifty Dollars 2 Twenty Dollars 1 Five Dollars 4 One Dollar 3 Quarter 2 Dime 4 Penny<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
As you can see on line 21 and 22 you can use either way.
Edit: changed code to resolve a nasty error due to rounding.
Hi Xander!!
Oh, that's neat. Thanks for the script. Nice use of modulus, and creative example of doing the same without it.
Here's a slide deck I used, giving an Intro to Arcade 30-min session at the Mid-Atlantic User Conference a few weeks ago: Arcade: an Introduction - Esri MUC 2019
Hi jbarry-esristaff ,
Thanks for sharing the slide deck. Very cool!
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.