c# to js conversion

I have trouble translating this peace of code from c# to js. here is the c# code:

public byte Block(int x, int y, int z){
  
 if( x>=worldX || x<0 || y>=worldY || y<0 || z>=worldZ || z<0){
  return (byte) 1;
 }
  
 return data[x,y,z];
}

and here is my js code:

function Block(x : int, y : int, z : int) : byte{

	if( x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z<0){
		return ???? 1;	
	}
	return data[x, y, z];
}

I would appreciate any help or suggestions.

Remove the ???, so it’s just

return 1;

It will be implicitly converted to byte, since that’s what the function returns.

I’m not a Javascript person, so the following is a guess (though, in the absence of any other responses, a guess is still better than nothing, right?)

Javascript does not support multidimensional arrays. You don’t include the line of code on which data is declared, but you certainly won’t be able to access it as data[x,y,z]. What you can do is create an array in which each element is an array (and then each element in that array is an array, etc. etc.). In which case, you’d access a 3rd-level element as

data[x][y][z]

As for the first problem, I believe you need to explicitly cast a variable of type byte at declaration. So, that would make the whole solution something more like:

function Block(x : int, y : int, z : int) : byte{

  var output : byte;

  if( x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z<0){
    output = 1;
  }
  else {
    output = data[x][y][z];
  }

return output;
}

If that doesn’t work, er… just use C# - it’s better :slight_smile: