Is array is cyclic or not ?
case 1:-
tasks: [“a”,”b”]
dependencies: [“a:b”]
result: Not cyclic
explanation : first “b” executes that “a” executes
case 2:-
tasks: [“a”,”b”,”c”]
dependencies: [“a:b”,”b:c”,”c:a”]
result: cyclic
explanation : a dependent on b , b dependent c again c dependent on a (looping)
case 3:-
tasks: [“a”, “b”, “c”,’d’,’e’]
dependencies: [“a:b”, “c:d”,’d:e’,’e:c’]
result: cyclic
explanation : c->d, d->e, e->c (loop)
Following is my code and it is not working for cyclic . someone please help me in this
const cyclicOrNot = () => {
var tasks = ["a", "b", "c",] ,
dependencies = ["a:b","b:c", "c:a",], result = []
var found = false
dependencies.map(i=>{
var dependentitem = i.split(':')[0];
var cyclicResult= checkCyclic(i,dependencies,dependentitem,found);
console.log(cyclicResult)
if(cyclicResult) {
return "cyclic"
}
else {
return "not cyclic"
}
})
}
const checkCyclic = (i,dep,item,found) => {
return dep.every(d=>{
if (i.split(':')[1]===d.split(':')[0]) {
if(item===d.split(':')[0]){
found = true
return found
}
else {
checkCyclic(d,dep,item,found);
}
return found
}
})
}