How do I convert this into C# and where should this be added?
I know JavaScript, but I´m new into C# , I could use some help. The function is JavaScript and the other code is in C#. The desired result is to make the text object to be the value of the "findPrimeNums" function output. How should I change the function, how do I call it and where should it be in the code? I want to merge these codes into one, C#. Thanks in advance.
The prime function is here:
function findPrimeNums(n) {
var x= 3,j = 1,i=2,
primeArr=[2],isPrime;
for (;x<=n;x+=2){
j = Math.sqrt (x);
isPrime = true;
for (i = 2; i <= j; i++)
{
if (x % i == 0){
isPrime = false;
break;
}
}
if(isPrime){
primeArr.push(x);
}
}
for (var e = 1; e < primeArr.length; e++) {
console.log(primeArr[e])
}
}
The rest (C# code) is here:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class TextChanger : MonoBehaviour {
//function here?
void Start () {
GameObject myTextGo = GameObject.Find("MyText");
Text textObject = myTextGo.GetComponent<Text>();
textObject.text = //function result
}
}
you might try some c# tutorials to get a little more experience about how things work - what you're asking (where you put it and how you call it) are some of the first things you'd learn...
regarding the code, did you look for code converters or try to convert it yourself? the syntax is remarkably similar...
hopefully you've done all of this in the 3 hours since you posted the question ;)
Answer by jmonasterio · Dec 30, 2015 at 06:32 PM
For a common algorithm like this, the best thing to do would be to google for a C# function that does what you want:
But, I just did a straighforward port. It's not very good code to start with, so you may want to throw this away and start over anyway. Maybe this will help you learn, though:
void findPrimeNums(int n)
{
var primeArr = new List<int>();
bool isPrime;
for (int x=3; x <= n; x += 2)
{
int j = (int) Mathf.Sqrt(x);
isPrime = true;
for (int i = 2; i <= j; i++)
{
if (x % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
primeArr.Add(x);
}
}
for (int e = 0; e < primeArr.Count; e++) // Starting at zero here. Original code probably skipped first.
{
print(primeArr[e]);
}
}
Add the above function where you had the comment:
//function here?
Also note that the function you supplied doesn't return any values, so you can't really use it like you want:
textObject.text = findPrimeNums(100); // Won't work.
Not really clear what you want to do, since the function you started with printed out the results to console ins$$anonymous$$d of returning anything.
Great answer, thanks for your help! I was just wondering, why doesn't the textObject.text = findPrimeNums(100);
work. Will adding .toString do the job or what should I do?
Your answer
Follow this Question
Related Questions
GameJolt API simple Score 0 Answers
Running C# Function from JS file 0 Answers
Any way to use UI on a screen renderer? 0 Answers
addComponent c# script by java 0 Answers
Jump Issue :( 1 Answer