Can you hack this? #1
Alfredo Salzillo

Alfredo Salzillo @alfredosalzillo

About: 🐺

Location:
Italy
Joined:
Nov 8, 2018

Can you hack this? #1

Publish Date: Mar 10 '21
22 12

Say that we have a function called nameEqualsToItself how accept a person object as input that checks if the name property of the input object equal the same name property of the input object implemented like this:

const nameEqualsToItself = (person) => person.name === person.name
Enter fullscreen mode Exit fullscreen mode

Can you find the input that makes nameEqualsToItself return false?

const personX = ???
console.log(nameEqualsToItself(personX)) // print `false`
Enter fullscreen mode Exit fullscreen mode

Comments 12 total

  • АнонимMar 10, 2021

    [deleted]

    • Alfredo Salzillo
      Alfredo SalzilloMar 10, 2021

      It print true :) try it

      • IroncladDev
        IroncladDevMar 11, 2021

        It printed false.

        const nameEqualsToItself = (person) => person.name === person.name
        const personX = { name: NaN, };
        console.log(nameEqualsToItself(personX))
        
        Enter fullscreen mode Exit fullscreen mode
  • Temani Afif
    Temani AfifMar 10, 2021
    const personX={};
    personX.name=NaN;
    
    Enter fullscreen mode Exit fullscreen mode

    OR simply

    const personX={name:NaN};
    
    Enter fullscreen mode Exit fullscreen mode

    a. If x or y are any of NaN, +∞𝔽, or -∞𝔽, return false. ref

    • Akash Kava
      Akash KavaMar 11, 2021

      It will return false only for NaN, because Number.POSITIVE_INFINITY === Number.POSITIVE_INFINITY returns true, and it is same for negative infinity as well.

  • Vaishali JS
    Vaishali JSMar 11, 2021

    NaN is never equal to itself 👍

  • Kins
    KinsMar 11, 2021
    const personX = { get name() { return Math.random() } };
    console.log(nameEqualsToItself(personX)); //This will return false as well 
    
    Enter fullscreen mode Exit fullscreen mode
  • Amin
    AminMar 12, 2021

    Using a Proxy.

    "use strict";
    
    const personNameEqualsItself = person => person.name === person.name;
    
    const person = (() => {
      let boolean = true;
    
      return new Proxy({}, {get: (target, property) => property === "name" ? boolean = !boolean : undefined});
    })();
    
    console.log(personNameEqualsItself(person)); // false
    console.log(personNameEqualsItself(person)); // false
    console.log(personNameEqualsItself(person)); // false
    
    Enter fullscreen mode Exit fullscreen mode
    • Richard Kichenama
      Richard KichenamaApr 25, 2021
      const personX = new Proxy({}, { get (target, attr) { return /name/i.test(attr) ? Symbol('name') : target[attr]; });
      
      Enter fullscreen mode Exit fullscreen mode
Add comment