0

//not the answer I want
var recur = (n, m) => {
    if (n || m === 1) {
        return 1;
    } else {
        return recur(n - 1, m) + recur(n, m - 1);
    }
};

//answer I want
var recur2 = (n, m) => {
    return 1 ? n || m === 1 : recur2(n - 1, m) + recur2(n, m - 1);
};

console.log(recur(2, 2));
console.log(recur(3, 3));
console.log(recur2(2, 2));
console.log(recur2(3, 3));

Results :

result


I am confused about the difference in answers. What's the difference in using conditional operator? I thought they were the same beside shorter syntax?

TiiJ7
  • 3,332
  • 5
  • 25
  • 37
sagorzaii
  • 16
  • 2
  • `1 ?` is like saying `if(1)`. The first thing is the condition. – VLAZ Jul 16 '21 at 07:07
  • In the "good one", you are basically always returning `n || m === 1`. It will ignore the second part of the conditional, because de condition is always `true` – Javi Marzán Jul 16 '21 at 08:49

4 Answers4

2

You have different logical operators in top and bottom functions. For the recur2 to work the same as recur you need to change this:

return 1 ? n || m === 1 : recur2(n - 1, m) + recur2(n, m - 1);

to:

return n || m === 1 ? 1 : recur2(n - 1, m) + recur2(n, m - 1);

In your example 1 is always truthy so, regardless of what arguments you pass there it returns the result of n || m === 1 which is n.

ischenkodv
  • 4,205
  • 2
  • 26
  • 34
0

This is how comparision works:

console.log(2||0)
console.log(0||1)
console.log(0||null)
console.log(Boolean(0||1))

The || operator will return the first truthy value or else the second value. They work inside the if(), because these are all truthy values : 1,2, "11" etc.

null, 0 etc. are falsy values.

In your first code, it auto returns from the first condition with 1 in both cases.

Should point out that in conditional operator the expression before ? is checked and if true first expression is returned else the second.

In your second code 1 ? n || m === 1: 1 is a truthy value so first condition will run always.

As mentioned above : (n || m === 1) will return n if it is truthy, or else will return the value of the expression m===1.

The correct code using conditional operator could be :

   return (n || m === 1) ? 1 : recur2(n - 1, m) + recur2(n, m - 1);
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • 1
    You should separate the list of truthy vs falsy operator better. You are separating them with the point of the etc., which makes it confusing and seem like all of those values belong to the same list. – Javi Marzán Jul 16 '21 at 08:58
0

The below should work,

var recur2 = (n, m) => {
    return n || m === 1 ? 1 : recur2(n - 1, m) + recur2(n, m - 1);
};

console.log(recur2(2, 2));
console.log(recur2(3, 3));
Javvy7
  • 146
  • 1
  • 8
0

Thank you ischenkodv for pointing out the error and everyone for your kindness. I just found out I made a terrible mistake by assuming n || m === 1 equals to n === 1 || m === 1. Correcting this solved the problem.

var recur = (n, m) => {
    if (n === 1 || m === 1) {
        return 1;
    } else {
        return recur(n - 1, m) + recur(n, m - 1);
    }
};

var recur2 = (n, m) => {
    return n === 1 || m === 1 ? 1 : recur2(n - 1, m) + recur2(n, m - 1);
};

console.log(recur(2, 2));
console.log(recur(3, 3));
console.log(recur2(2, 2));
console.log(recur2(3, 3));
sagorzaii
  • 16
  • 2