Monte Carlo simulations

This is more a question about JavaScript rather than Ionic - it is just that I am using Ionic and have the issue.

Basically, I am using a series of for loop and lookups to calculate the allocation of staff to tasks.

All works fine until I start running multiple tries of the code with random selections to try and find the best solution.

The code then produces unexpected values and it seems that values from one set of procedure are leaking into another set.

I would publish the code here but to be honest, it won’t help much. I am not asking how to debug the code but basically how to run simulations within Ionic.

Has anyone got any suggestions - could I use a different language with Ionic 4?

Have you tried using karma?

post related code.

Best regards, anna-liebt

Sorry @anna_liebt the code is 300 odd lines is undocumented and also very rough - I would be ashamed to publish in it’s current state.

I believe the problem is with javascript - it keeps on running and does not wait for the lookup and loops to be completed.

Maybe there is a “breach” in one of your promise or observable in your code?

Also, if possible, maybe you could try to reduce your code and add piece after piece to see when it will begin to crack (like 20 lines of code, test, 40 lines of code, test, etc.)?

Not sure that the answer you are expecting and maybe I don’t get your problem at all, but I hope it might give an idea

About your question “could I use a different language with Ionic 4” as far as I understand it will be framework agnostic but all of them based on javascript (someone correct me if I’m wrong)


okay, I understand. Makes much easier to help. Look to line 143, charakter 24 and change it to &. If this not helps then I guess it is line 231.

By the way, I do not believe, that related code is 300lines.

Best regrads, anna-liebt

@anna_liebt You were right there was a bug in the code.
Not sure how it was doing it but making a deep copy of an array called ‘data’ using


and then updating the cloned array it was also updating the original array - which you would not expect to happen.
I think the reason why was that I was passing the cloned array into the function calls. When I cleaned up the code and made the cloned array as an object within the class the breach went away.
It was as if javascript was trying to unpick the references passed to it in the function calls.
I am just glad that it works now!

Many thanks for the advice from everyone who responded to this question.

1 Like