DEV Community

Alfredo Salzillo
Alfredo Salzillo

Posted on

12 8

Can you hack this? #1

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

Top comments (12)

Collapse
 
afif profile image
Temani Afif • Edited
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

Collapse
 
akashkava profile image
Akash Kava

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.

Collapse
 
alfredosalzillo profile image
Alfredo Salzillo

Also if using a Getter or a Proxy returning different values each time :)

Thread Thread
 
atulcodex profile image
🚩 Atul Prajapati 🇮🇳

You don't know everything 😛

Collapse
 
kinzmak profile image
Kins • Edited
const personX = { get name() { return Math.random() } };
console.log(nameEqualsToItself(personX)); //This will return false as well 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
shane profile image
Shane McGowan • Edited

This is big brain, love it

Collapse
 
aminnairi profile image
Amin

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
Collapse
 
rkichenama profile image
Richard Kichenama
const personX = new Proxy({}, { get (target, attr) { return /name/i.test(attr) ? Symbol('name') : target[attr]; });
Enter fullscreen mode Exit fullscreen mode
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
alfredosalzillo profile image
Alfredo Salzillo

It print true :) try it

Collapse
 
ironcladdev profile image
Conner Ow

It printed false.

const nameEqualsToItself = (person) => person.name === person.name
const personX = { name: NaN, };
console.log(nameEqualsToItself(personX))
Enter fullscreen mode Exit fullscreen mode

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay