How to convert this C# scripts to Javascript

411 views Asked by At

I have a C# script like below:

public List<MazePath> BreakIntoConnectedPaths()
{
    List<MazeVertex> remainVertices = new List<MazeVertex>(vertices);
    List<MazePath> paths = new List<MazePath>();
    while (remainVertices.Count > 0)
    {
        MazePath path = new MazePath();
        path.entrancePosition = entrancePosition;
        path.exitPosition = exitPosition;
        VisitCell(path, remainVertices.First(), null, remainVertices);
        paths.Add(path);
        //Store the coordinate for entrance and exit 
    }
    return paths;
}

void VisitCell(MazePath path, MazeVertex ver, MazeVertex parent, List<MazeVertex> remainVertices)
{
    remainVertices.Remove(ver);
    path.Add(ver);
    for (int i = 0; i < ver.connectVertices.Count; i++)
    {
        MazeVertex ver2 = ver.connectVertices[i];
        if (ver2 != parent)
        {
            VisitCell(path, ver2, ver, remainVertices);
        }
    }
}

I want to convert it to javascript as below

BreakIntoConnectedPaths = function() {
    var remainVertices = _.cloneDeep(this.vertices);
    var paths = [];

    while (remainVertices.length > 0) {
        var path = new Path();
        path.entrancePos = this.entrancePos;
        path.exitPos = this.exitPos;
        this.VisitCell(path, remainVertices[0], null, remainVertices);
        paths.push(path);
        // Store the coordinate for entrance and exit
    }
    return paths;
}

VisitCell = function(path, vertex, parentVertex, remainVertices) {
    _.remove(remainVertices, function(v) {
        return v.x === vertex.x && v.z === vertex.z;
    });
    path.Add(vertex);
    for (var i = 0; i < vertex.connectVertices.length; i++) {
        var connectedVertex = vertex.connectVertices[i];
        // if (parentVertex && (connectedVertex.x !== parentVertex.x || connectedVertex.z !== parentVertex.z)) {
        if(parentVertex && _.isEqual(connectedVertex, parentVertex)) {
            VisitCell(path, connectedVertex, vertex, remainVertices);
        }
    }
}

The _ symbol here is lodash sign.

After I convert to javascript code, the behavior of these functions is difference with the C# one. With the same vertices data, the paths array had returned with difference size. Thanks you for reading and pls help me if you see my mistake here.

2

There are 2 answers

1
adam0101 On BEST ANSWER

In the C# version, your VisitCell function has a condition that says if(ver2 != parent), but in the JS version you check that they are equal instead of not equal.

Also, that condition would never pass any way because in your first call to that function you pass in null for the parent, but in that condition you check that the parent is "truthy".

Lodash's isEqual can handle null values, so I'm not sure why you're checking if the parent is truthy there. Perhaps you meant to do this?

 if(!_.isEqual(connectedVertex, parentVertex)) {
2
rabelloo On

There are several ways to improve your JavaScript code. When transpiling code, it is better to not copy/paste and fix, but to rewrite using the target language instead.

I would prefer to have this written, for example:

var vertices;
var entrancePos;
var exitPos;

function Path(entrancePos, exitPos){
    this.entrancePos = entrancePos;
    this.exitPos = exitPos;

    this.Add = function() {
        // your Add() code here
    }
}

function breakIntoConnectedPaths() {
    var remainingVertices = _.cloneDeep(vertices);
    var paths = [];

    while (remainVertices.length) {
        var path = new Path(entrancePos, exitPos);
        visitCell(path, remainingVertices.shift());

        // Store the coordinate for entrance and exit
        paths.push(path);
    }

    return paths;
}

function visitCell(path, vertex, parentVertex) {
    path.Add(vertex);

    for (var i = 0; i < vertex.connectVertices.length; i++) {
        var connectedVertex = vertex.connectVertices[i];
        if(_.isEqual(connectedVertex, parentVertex)) {
            visitCell(path, connectedVertex, vertex);
        }
    }
}

Keep in mind that the variables vertices, entrancePos, exitPos and Path are not available to me on your C# code, so I only declare them on JavaScript. Implement them as you may.

Does that fix it, by the way?