Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Challenge: Use Multiple Conditional (Ternary) Operators. #38872

Closed
FelixBoscoJ opened this issue May 22, 2020 · 16 comments · Fixed by #38993
Closed

Bug in Challenge: Use Multiple Conditional (Ternary) Operators. #38872

FelixBoscoJ opened this issue May 22, 2020 · 16 comments · Fixed by #38993
Labels
first timers only Open for anyone doing contributions for first time. You do not need permission to work on these. help wanted Open for all. You do not need permission to work on these. scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory.

Comments

@FelixBoscoJ
Copy link

Describe your problem and how to reproduce it:
Bug - Test for below code fails with error: checkSign should use multiple conditional operators.

function checkSign(num) {
  return (num!==0) ?(num>0) ? "positive" 
    : "negative" 
    : "zero";
}


console.log(checkSign(10));
console.log(checkSign(-10));
console.log(checkSign(0));

Screenshot -1:
image

Screenshot - 2:
image

How to reproduce It.
Instead of Error: “checkSign should use multiple conditional operators.”
Error should be: Using multiple ternary operators in statement-if-true part of ternary operator is not best practice.

(Increasing clarity of error message)

Add a Link to the page with the problem:
Link to Challenge;
Link to my post reporting bug on forum:

@FelixBoscoJ FelixBoscoJ added scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. status: waiting triage This issue needs help from moderators and users to reproduce and confirm its validity and fix. labels May 22, 2020
@jeremylt
Copy link
Member

I agree that some tweaking makes sense. I'd would help if we can capture the fact that the test fails if a) you aren't using a multiple ternary or b) the ternary isn't in the expected format.

@ShaunSHamilton
Copy link
Member

I do not think this is what the challenge asks for: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

This is the code from the docs:

function example(…) {
    return condition1 ? value1
         : condition2 ? value2
         : condition3 ? value3
         : value4;
}

What you have constructed is:

function checkSign(num) {
  if (num!==0) {
    if (num>0) {
      return "positive";
    } else {
      return "negative";
    }
  } else {
    return "zero";
  }
}

I agree, you have accomplished the task. However, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {
  if (a === b) {
    return "a and b are equal";
  }
  else if (a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

So, perhaps we should change the challenge instructions to be more specific as to the format...

@FelixBoscoJ
Copy link
Author

FelixBoscoJ commented May 23, 2020

I do not think this is what the challenge asks for: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

This is the code from the docs:

function example(…) {
    return condition1 ? value1
         : condition2 ? value2
         : condition3 ? value3
         : value4;
}

What you have constructed is:

function checkSign(num) {
  if (num!==0) {
    if (num>0) {
      return "positive";
    } else {
      return "negative";
    }
  } else {
    return "zero";
  }
}

I agree, you have accomplished the task. However, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {
  if (a === b) {
    return "a and b are equal";
  }
  else if (a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

So, perhaps we should change the challenge instructions to be more specific as to the format...

Sir, please note: this repository is of Free Code Camp. Thank You for your reply.

@ShaunSHamilton
Copy link
Member

@FelixBoscoJ , haha. Yes, I am well aware. I linked to the Mozilla documentation to retrieve an example of Multiple Conditional Ternary Operators.

My point is still entirely valid.

@FelixBoscoJ
Copy link
Author

FelixBoscoJ commented May 23, 2020

@FelixBoscoJ , haha. Yes, I am well aware. I linked to the Mozilla documentation to retrieve an example of Multiple Conditional Ternary Operators.

My point is still entirely valid.

function checkSign(num) {
  return (num!==0) ?(num>0) ? "positive" 
    : "negative" 
    : "zero";
}


console.log(checkSign(10));
console.log(checkSign(-10));
console.log(checkSign(0));

Please try the above code in this challenge. Please check what is required to pass the challenge.

@ShaunSHamilton
Copy link
Member

@FelixBoscoJ , I have, and I understand exactly why this issue was created. I am pointing out that the format of your solution is not what is expected, which is clearly seen in the example given in the lesson:

function checkSign(num) {
  if (num!==0) {
    if (num>0) {
      return "positive";
    } else {
      return "negative";
    }
  } else {
    return "zero";
  }
}

I agree, you have accomplished the task. However, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {
  if (a === b) {
    return "a and b are equal";
  }
  else if (a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

Your solution does not contain else if logic.

@FelixBoscoJ
Copy link
Author

FelixBoscoJ commented May 23, 2020

@FelixBoscoJ , I have, and I understand exactly why this issue was created. I am pointing out that the format of your solution is not what is expected, which is clearly seen in the example given in the lesson:

function checkSign(num) {
  if (num!==0) {
    if (num>0) {
      return "positive";
    } else {
      return "negative";
    }
  } else {
    return "zero";
  }
}

I agree, you have accomplished the task. However, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {
  if (a === b) {
    return "a and b are equal";
  }
  else if (a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

Your solution does not contain else if logic.

Sir the condition to win the challenge is:

Use multiple conditional operators in the checkSign function to check if a number is positive, negative or zero. The function should return "positive", "negative" or "zero".

We should Use Multiple Conditional (Ternary) Operators.

@jeremylt
Copy link
Member

Perhaps the error message should be "... multiple conditional operators in the recommended format."

And perhaps adding a note along the lines of "It's is a best practice to format the multiple conditional operators so that each condition is on a separate line, as shown in the example, for readability."

@ShaunSHamilton
Copy link
Member

"It's is a best practice to format the multiple conditional operators so that each condition is on a separate line, as shown in the example, for readability."

I would be for adding a note similar.

@jeremylt
Copy link
Member

Closed? Was an update to the description merged?

@FelixBoscoJ FelixBoscoJ reopened this May 25, 2020
@FelixBoscoJ
Copy link
Author

FelixBoscoJ commented May 25, 2020

@SKY020 It is not possible to implement if/else if/else logic using ternary Operator. But the task can be accomplished.

I agree, you have accomplished the task. However, the expected implemented logic is of the form:

 function findGreaterOrEqual(a, b) {
   if (a === b) {
     return "a and b are equal";
   }
   else if (a > b) {
     return "a is greater";
   }
   else {
     return "b is greater";
   }
 }

No, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {

if (a === b) {
   return "a and b are equal";
 }
 else {
   if (num>0) {
     return "positive";
   } else {
     return "negative";
   }  
 }
}

@jeremylt
Copy link
Member

@FelixBoscoJ

Expected solution (one of 6, very similar to your final answer):

function checkSign(num) {
  return (num > 0) ? "positive" 
    : (num < 0) ? "negative"
    : "zero";
}

If-else version

function checkSign(num) {
  if (num > 0) {
    return "positive";
 } else if (num < 0) {
    return "negative";
 } else {
    return "zero";
 } 
}

A ternary, or multiple ternary, always can be written with if-else. The challenge and the description is accurate. We just want to add a clarifying comment or two to emphasize the best practice formatting for readability.

@FelixBoscoJ
Copy link
Author

Nevermind.

@ojeytonwilliams
Copy link
Contributor

It looks to me like adding clarifying comments is the best solution. So, let's leave this open until a PR gets merged.

@ojeytonwilliams ojeytonwilliams added first timers only Open for anyone doing contributions for first time. You do not need permission to work on these. help wanted Open for all. You do not need permission to work on these. and removed status: waiting triage This issue needs help from moderators and users to reproduce and confirm its validity and fix. labels May 27, 2020
@jasnkwcz
Copy link
Contributor

I'd like to jump on this, if possible. I'm a first-timer but really excited to learn!

@ojeytonwilliams
Copy link
Contributor

Go for it, @jasnkwcz. This should be a good issue for a first-timer.

I recommend taking a look at the contribution guide, and if you get stuck ask away in the contributor's chat room

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
first timers only Open for anyone doing contributions for first time. You do not need permission to work on these. help wanted Open for all. You do not need permission to work on these. scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory.
Projects
None yet
5 participants