How do I write code with a ternary operator?

  • 0
    Hello, please help!
    It is necessary to write the code for the task (x) function that uses the ternary operator in order to return the answer as a string, whether the proposed value is null (returns' null '), or undefined (returns' undefined'), or NaN (returns' not a number '). Otherwise, the value 'Other' should be returned. For example, the value '#' should return 'Other'. The solution should be one line.

    item == null? 'null': item == undefined? 'undefined': item == NaN? 'not a number': 'Other'; (something wrong)
    JavaScript Elizabeth White, Jan 19, 2019

  • 3 Answers
  • 0
    There is a moment that NaN is not equal to itself. Those. NaN === NaN will be false.

    And so, below should be a working version.

    const task = (item) =>
    (item === null && 'null') ||
    (item === undefined && 'undefined') ||
    (item.toString() === 'NaN' && 'not a number') ||
    Audrey Cobb

  • 0
    const task = x => (x === null && "null") || (x === undefined && "undefined") || (isNaN(x) && "not a number") || "Other";
    Xavier Shields

  • 0
    task = x = & gt; x === null? 'null': x === undefined? 'undefined': Number.isNaN (x)? 'not a number': 'Other';

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!