What is the meaning of "-" when it is next to a variable or parameter

1

I was reading the book of Eloquent Javascript, and in the exercises of Functions of recursion it makes you solve a problem and this is the solution they give in the book

function isEven(n) {
  if (n == 0)
    return true;
  else if (n == 1)
    return false;
  else if (n < 0)
    return isEven(-n);
  else
    return isEven(n - 2);
}


console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → false

My question is what does the use of "-n" mean in this line?

return isEven(-n);

and what are its possible uses

    
asked by Guillermo Fragachan 27.07.2016 в 15:32
source

3 answers

2

It has the same value as in mathematics in this case.

For example:

Previously they must have defined the value of the variable n, in that case, if n = 10, -n = -10, and n-2 = 8.

That loop is a recursive exercise to finish, according to the initial value of n returning a Boolean variable to the isEven function. Actually it will return false if n was an even number, and true if it was odd.

    
answered by 27.07.2016 / 15:39
source
1

Simply change the sign. If in the variable n we have the value 20, the method isEven() will be passed as a parameter the value -20 .

Possible uses:

  • Change the sign of the value of a variable: -)
answered by 27.07.2016 в 15:35
1

EDIT : To be short:

a% sign - in front of a numeric variable what it does is change that variable.

That is, if the variable is positive, it becomes negative. If the variable is negative, it becomes positive. Example:

n = 5 implies that -n = -5

n = -5 implies that -n = 5

Original reply

In the defined function isEven uses recursion for the natural numbers i positive .

Notice that we have 4 cases defined in the function. 2 bases and 2 continuation.

In both base cases, the end of the function is defined, where if it is arrived with n=0 then it returns true and if it is reached with n=1 it returns false .

The operation of the function itself is to call the same function by subtracting 2 from each iteration until it reaches 0 or 1 (what the last function does).

As it is only defined by positive numbers, there is a base case of input that calls the function isEven if the number we want is negative. To give an example:

  • We start from n=5 :

1st iteration: isEven(3) is called

2nd iteration: isEven(1) is called

3rd iteration: How n=1 returns false .

  • If we start from any negative number, for example, n=-5 , which is the specific case you ask:

1st iteration: isEven(5) is called

2nd iteration: isEven(3) is called

3rd iteration: isEven(1) is called

4th iteration: How n=1 returns false .

So doing a isEven of a negative number is the same as doing a isEven of the same number but positive (and with 1 more level of recursion).

Of the 3 example functions given in the book the last console.log(isEven(-1)); is the only one that enters in that specific case ( n<0 ) and then calls isEven(1)

To finish simply commenting that it is much easier to use the module to calculate if a number is even or odd:

function isEven(n) 
{
    return n%2==0;
}
    
answered by 27.07.2016 в 15:44