I am working on sequential and parallel programming in javascript, I was able to solve sequential programming but didn't have an idea of how to do the same using parallel programming.
For Sequential problem was :
This is an example of sequential processing where it will start at 1 and go till 3 and add 21 to it. Then it will start at 1 and go till 2 and add 10 to it. In the end, start at 1 and go till 4 and add 1 to it.
For Input : 1 3*21#2*10#4*1
output will be :
22
23
24
11
12
2
3
4
5
I solved using below code
function solution(inputData) {
var first = inputData.substring(0, 1);
if(first == 1)
{
//sequential
var strArr = inputData.split(" "); //3*21#2*10#4*1
var strHashSplitArr = strArr[1].split("#"); //3*21 #2*10# 4*1
for(var i=0;i<strHashSplitArr.length;i++)
{
var loopInp = strHashSplitArr[i].split("*");
var maxVal = parseInt(loopInp[0]);
var addVal = parseInt(loopInp[1]);
for(var k=1;k<=maxVal;k++)
{
console.log(k+addVal);
}
}
}
}
But now the problem is with parallel programming
Problem:
For example 2, there are 3 processes to start in parallel and numbers are 1, 2 and 3 with delays 100, 20 and 50 consecutively. Here all the processes will start together but a number with less delay will be printed first. Here the number with less delay is 2.So it will print 21,22 in the meantime 50 ms will be achieved and it will print 51 from 3rd number. Now it is mixed with number 1 and prints 101 and so on.
Input : 2 1*100#2*20#3*50
Output should be :
21
22
51
101
52
53
I didn't try using parallels but sorted with milliseconds but couldn't get the expected output.
Here is JSfiddle code for 2nd .. which is giving wrong output (I am not using parallel approach) : https://jsfiddle.net/mahajan344/0u2ka981/
How can I achieve the same output using parallel JavaScript programming?