I don't understand the usage of code where there's no block of code after if (..isSimulation) that instead just does return after which it follows up with an actual code that is suppose to access restful api from another server.
Right so:
if (this.isSimulation) return;
var charsInfo = new Array();
_keys.forEach(function (key, index, array) {
EveOnline.fetch('account:Characters', { keyID: key.key1, vCode: key.key2 }, Meteor.bindEnvironment(function(err, result) {
if (err) {
In most C-based languages you can omit the code block ({ and } brackets) if the inner block evaluates to one statement.
For instance this:
if (x == 0) {
x++;
}
and this:
if (x == 0)
x++;
do exactly the same thing. This works also for nested conditionals, like in your example.
So this:
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (i>j) {
x++;
}
}
}
and this:
for (i=0; i<10; i++)
for (j=0; j<10; j++)
if (i>j)
x++;
are also the same thing. The nested conditional/loop is seen as a single evaluated "thing" until it gets to the end. Basically, the brackets defining the code blocks are assumed in the second example.
What you can't do is have a multi line code block not be surrounded in curlies.
For instance this:
if (i>j) {
x++;
y++;
}
And this:
if (i>j)
x++;
y++;
do NOT do the same thing at all. In the first case, x and y are both incremented only if i is greater than j. In the second case, x will be incremented only if i is greater than j, but y gets incremented all the time as it's not part of the if conditional before it. This is because x++ is considered one "statement" and y++ is considered another, and only the first statement is assumed to be part of the proceeding if block.
What does the function return when condition is terminated?
int power(int a,int b)
{
if (b != 1)
return a * power(a, b-1);
}
well i think (a) and (b) have a rubbish value inside.It means that the loop is continuing until the a * (power-1) change to 1 and finally it return true.. and loop will end.
I am trying to write a simple mutually recursive function in Haxe 3, but couldn't get the code to compile because whichever one of the mutual functions that appears first will report that the other functions in the group is undefined. A minimal example is below, in which mutually defined functions odd and even are used to determine parity.
static public function test(n:Int):Bool {
var a:Int;
if (n >= 0) a = n; else a = -n;
function even(x:Int):Bool {
if (x == 0)
return true;
else
return odd(x - 1);
}
function odd(x:Int):Bool {
if (x == 0)
return false;
else
return even(x - 1);
}
return even(a);
}
Trying to compile it to neko gives:
../test.hx:715: characters 11-14 : Unknown identifier : odd
Uncaught exception - load.c(181) : Module not found : main.n
I tried to give a forward declaration of odd before even as one would do in c/c++, but it seems to be illegal in haxe3. How can one define mutually-recursive functions like above? Is it possible at all?
Note: I wanted to have both odd and even to be local functions wrapped in the globally visible function test.
Thanks,
Rather than using the function myFn() {} syntax for a local variable, you can use the myFn = function() {} syntax. Then you are able to declare the function type signiatures before you use them.
Your code should now look like:
static public function test(n:Int):Bool {
var a:Int;
if (n >= 0) a = n; else a = -n;
var even:Int->Bool = null;
var odd = null; // Leave out the type signiature, still works.
even = function (x:Int):Bool {
if (x == 0)
return true;
else
return odd(x - 1);
}
odd = function (x:Int):Bool {
if (x == 0)
return false;
else
return even(x - 1);
}
return even(a);
}
This works because Haxe just needs to know that even and odd exist, and are set to something (even if it's null) before they are used. We know that we'll set both of them to callable functions before they are actually called.
See on try haxe: http://try.haxe.org/#E79D4
I've been running through codeacademy's tutorials and projects. On FizzBuzz++ 1.5 they want us to re-write the "Wobble" function as Wob, using ternary operators. I keep getting an error saying "missing operand" with the following code. Also the +1 on the end of the return, how does that work, does javaScript store it as a temp value because it doesn't get assigned to any var. Thanks for the help. Code is below.
var Wibble = {
Wobble: function(a, b) {
if(a===b)
//if the variables are equal return 0
return 0;
else {
//decrement the larger of the 2 values
if (a>b) {
a--;
} else {
b--;
}
//call this function again with the decremented values
//once they are equal the functions will return up the stack
//adding 1 to the return value for each recursion
return Wibble.Wobble(a,b)+1;
}
},
//This is the line of code that doesn't want to function..
Wob: function(a, b) {
(a===b) ? return 0 :(a<b) ? return this.Wob(a--,b)+1 : return this.Wob(a,b--)+1;
}
};
The following expression with the ternary operator:
result = (a) ? x : y;
is equivalent to the following:
if(a)
{
result = x;
}
else
{
result = y;
}
Note the syntactical difference, where in the ternary operator you are switching in the assignment, whereas in the if statement syntax, you are assigning in the switch.
That is to say that:
(a == b) ? return 0 : return 1;
is not equivalent to:
if(a == b)
return 0;
else
return 1;
Instead, you would want to write:
return (a == b) ? 0 : 1;
public static boolean palindrome(String input, int i, int j)
{
if (i >= j)
return true;
if (input.charAt(i) == input.charAt(j))
{
i++;
j--;
palindrome(input, i, j);
}
else if (input.charAt(i) != input.charAt(j))
return false;
}
My Java platform (eclipse) won't accept this code as working, due to a "lack of return type." Now I know in proper coding ediquite, it's better to use only one return value, but when it comes to recursion, this is somewhat new to me. How can I go about doing this? If I instantiate a Boolean type at the top of this method, it's creating a new instance of that variable (and instantiating it as null or whatever I set it to) each time the method runs, but if I place it above my constructor, my method won't assign a value to it/can't return it.
Basically, how do I go about modifying my code to have a single return value that Eclipse will accept as always executing? I can do this easily enough with loops, but I'm not sure how to approach the topic with Recursion.
Just for improved readability:
public static boolean palindrome(String input, int i, int j)
{
if (i >= j)
return true;
if (input.charAt(i) != input.charAt(j))
return false;
return palindrome(input, i + 1, j - 1);
}
If we could use C# and Linq (since I don't have Java dev environment here) to reversing a char array:
public static bool palindrome(String input)
{
return input.Equals(new String(input.ToCharArray().Reverse().ToArray()));
}
The issue is that you have no return inside the second if statement. If charAt i and j are equal you never return anything.
Keeping the spirit of your code I'd shorten the whole thing up to be:
public static boolean palindrome(String input, int i, int j)
{
if (i >= j)
{
return true;
}
if (input.charAt(i) == input.charAt(j))
{
return palindrome(input, i + 1, j - 1);
}
return false;
}
You can certainly just do this:
return palindrome(input, i, j);
However it is good practice to have a single return to improve readability. Try this on for size:
boolean isPalindrome = false;
if (i >= j)
isPalindrome = true;
else if (input.charAt(i) == input.charAt(j))
{
i++;
j--;
isPalindrome = palindrome(input, i, j);
}
else if (input.charAt(i) != input.charAt(j))
isPalindrome = false;
return isPalindrome;
}
Have that boolean always instantiated. The key here is to make palindrome's return be stored in that boolean.
The recursive portion comes in at the call to palindrome. It will only finally return the final value after all of the recursive calls, because it only ever determines if its a palindrome when it reaches the end of the recursive cycle.
In the second if block, you don't return anything.
Just change the recursive call so that you return its value:
return palindrome(input, i, j);
Also, incidentally, you don't need to do i++ and j-- in the if block-- you can instead call the palindrome method with i+1 and j-1 instead, and that will have basically the same effect.
public static String palindrome(String input, int i, int j)
{
if (i >= j)
return "-1";
//--------------------------------------------//
if (input.charAt(i) == input.charAt(j))
{
i++;
j--;
//--------------------------------------------//
palindrome(input, i, j);
return "is palindrom";
}
//--------------------------------------------//
else
return "not palindrom";
}
}
//--------------------------------------------//
An other option might be this:
boolean isPalindrome(String s) {
boolean ret = true;
if (s.length() == 1 || s.equals("")) {
return ret;
} else {
char first = s.charAt(0);
char last = s.charAt(s.length() - 1);
if (first != last)
ret = false;
else if (s.length() > 2) {
String partial = s.substring(1, s.length() - 1);
ret = isPalindrome(partial);
}
}
return ret;
}
Probably this answer can help(in Python):
def isPalinr(str1):
if len(str1)<=1:
print('Palindrome')
return
if (str1[0] != str1[-1]):
print ('Not palindrome')
return
else:
return isPalinr(str1[1:len(str1)-1])
This will return None though we can return string in first 'if' clause because program will ultimately stop at that point.